<h2>Explanation for answer</h2>
<p>Since the craftcms supplied CLI file aka <code>./craft</code> uses the following to identify it's location:</p>
<pre><code>// Set path constants
define('CRAFT_BASE_PATH', __DIR__);
// the above finds out the file path where the ./craft file is stored in
</code></pre>
<p>There's no need for any <code>cd</code> commands in your crontab, so all you have to do, is make sure that you either have your <code>./craft</code> file marked as executable (<code>chmod +x path/to/craft</code>) or explicitly call it with <code>php</code> or <code>usr/bin/env php</code></p>
<p>Since according to your observations <code>/home/USERNAME/CRAFTROOT/craft backup</code> doesn't work in cron, I'd suggest using the following instead</p>
<h2>TLDR: (solution)</h2>
<p><code>/usr/bin/env php /home/USERNAME/CRAFTROOT/craft backup</code></p>
<h3>Potential causes of issue</h3>
<p>Now I won't guarantee that this was the cause, but it might just be the case.</p>
<ol>
<li>The User running the cronjobs was different compared to the one you used in ssh =&gt; different permissions =&gt; somehow broke execution through cron.</li>
<li>The <code>/home/USERNAME/CRAFTROOT/craft</code> file was not marked as executable (fix -&gt; <code>chmod +x /home/USERNAME/CRAFTROOT/craft</code>), though in that case one of your ssh commands should've also failed.</li>
<li>The different environment in Bash/Shell (for example, a different <code>$PATH</code> or the <code>.bashrc</code> not being read) in cron compared to ssh may also have been the cause behind this issue.</li>
</ol>
<p>Other than that I'm not sure what could've caused the issue...<br/>
<strong>Q:</strong> But just to be sure, (please add a comment as answer), you ran your ssh commands without the <code>#</code> infront of them, right? Otherwise it'd make sense that your ssh commands wouldn't output any failures since your commands would simply be read as comments. [<strong>A</strong> in comments]</p>
<h3>How to figure out what the cause was</h3>
<p>Adjust your non-working crontabs by adding <code> &gt;&gt; /home/USERNAME/CRAFTROOT/cron.log</code> to the back of the cronjob, this'll store the output of your command into the file located at <code>/home/USERNAME/CRAFTROOT/cron.log</code>, this would help in identifying what caused your issue in the first place.</p>

CC BY-SA 4.0
2019-12-10T16:52:16.047Z
That did it! Thanks.  Can you help me understand why it's necessary to specify PHP?  Is the Craft CLI only executable via PHP?
thisanimus
CC BY-SA 4.0
2019-12-11T07:35:12.703Z
I've edited the answer two include two potential causes of your issue, though there definitely might be more potential causes, these two seem quite plausible (especially the first one). Please read through them and also add a reply comment containing the answer to the question I've added in the edit (@ the bold font **Q**)
Campiotti
CC BY-SA 4.0
2019-12-11T19:06:49.347Z
• 1. Cron jobs are executed under the same user as SSH
 • 2. I did mod ./craft to be executable.  That allowed me to execute via ssh, but did not affect execution via cron.
 • 3. No, I did not include the # when actually running the commands.
thisanimus
CC BY-SA 4.0
2019-12-12T07:32:27.007Z
Well in that case I'm leaning towards the 3rd potential cause which I just remembered could've also caused this - different environment in terms of read/unread `.bashrc` and different `$PATH` contents leading to php potentially not even being found? Or maybe the `php` you were executing was differnt to the one found in `/usr/bin/env` thus leading to nothing happening.


If you want to see what the shell spewed out when it failed your previous commands, you could try doing `non-working-command >> /home/USERNAME/CRAFTROOT/cron.log` to have it's output stored in a file to read later on.
Campiotti
CC BY-SA 4.0
2019-12-09T09:31:56.103Z
2019-12-12T07:34:58.610Z
2020-06-17T08:56:27.903Z
Campiotti
<p>I'm trying to execute Craft 3.3.X CLI commands via cron. I can execute ./craft from ssh no problem, but the same type of commands triggered via cron do not execute.</p>

<p><strong>These command works via SSH:</strong></p>

<pre><code># cd /home/USERNAME/CRAFTROOT
# ./craft backup


# /home/USERNAME/CRAFTROOT/craft backup
</code></pre>

<hr>

<p><strong>These commands do nothing via cron:</strong></p>

<pre><code>cd /home/USERNAME/CRAFTROOT;./craft backup


/home/USERNAME/CRAFTROOT/craft backup
</code></pre>

<p>Any ideas?</p>

<craft3><cli><cron>
Is it possible to execute ./craft via cron?
# Is it possible to execute ./craft via cron?

(Asked by: username_0 on 2019-12-08)

I'm trying to execute Craft 3.3.X CLI commands via cron. I can execute ./craft from ssh no problem, but the same type of commands triggered via cron do not execute.
These command works via SSH:
# cd /home/USERNAME/CRAFTROOT
# ./craft backup


# /home/USERNAME/CRAFTROOT/craft backup


These commands do nothing via cron:
cd /home/USERNAME/CRAFTROOT;./craft backup


/home/USERNAME/CRAFTROOT/craft backup

Any ideas?

**Tags:** craft3, cli, cron

---

## 1 answers

### Answer by username_1 on 2019-12-09. Score: 1 | Accepted answer

Explanation for answer
Since the craftcms supplied CLI file aka ./craft uses the following to identify it's location:
// Set path constants
define('CRAFT_BASE_PATH', __DIR__);
// the above finds out the file path where the ./craft file is stored in

There's no need for any cd commands in your crontab, so all you have to do, is make sure that you either have your ./craft file marked as executable (chmod +x path/to/craft) or explicitly call it with php or usr/bin/env php
Since according to your observations /home/USERNAME/CRAFTROOT/craft backup doesn't work in cron, I'd suggest using the following instead
TLDR: (solution)
/usr/bin/env php /home/USERNAME/CRAFTROOT/craft backup
Potential causes of issue
Now I won't guarantee that this was the cause, but it might just be the case.

The User running the cronjobs was different compared to the one you used in ssh => different permissions => somehow broke execution through cron.
The /home/USERNAME/CRAFTROOT/craft file was not marked as executable (fix -> chmod +x /home/USERNAME/CRAFTROOT/craft), though in that case one of your ssh commands should've also failed.
The different environment in Bash/Shell (for example, a different $PATH or the .bashrc not being read) in cron compared to ssh may also have been the cause behind this issue.

Other than that I'm not sure what could've caused the issue...
Q: But just to be sure, (please add a comment as answer), you ran your ssh commands without the # infront of them, right? Otherwise it'd make sense that your ssh commands wouldn't output any failures since your commands would simply be read as comments. [A in comments]
How to figure out what the cause was
Adjust your non-working crontabs by adding  >> /home/USERNAME/CRAFTROOT/cron.log to the back of the cronjob, this'll store the output of your command into the file located at /home/USERNAME/CRAFTROOT/cron.log, this would help in identifying what caused your issue in the first place.

> **Comments:**
> **username_0**: That did it! Thanks.  Can you help me understand why it's necessary to specify PHP?  Is the Craft CLI only executable via PHP?  
> **username_1**: I've edited the answer two include two potential causes of your issue, though there definitely might be more potential causes, these two seem quite plausible (especially the first one). Please read through them and also add a reply comment containing the answer to the question I've added in the edit (@ the bold font **Q**)  
> **username_0**: • 1. Cron jobs are executed under the same user as SSH  • 2. I did mod ./craft to be executable.  That allowed me to execute via ssh, but did not affect execution via cron.  • 3. No, I did not include the # when actually running the commands.  
> **username_1**: Well in that case I'm leaning towards the 3rd potential cause which I just remembered could've also caused this - different environment in terms of read/unread `.bashrc` and different `$PATH` contents leading to php potentially not even being found? Or maybe the `php` you were executing was differnt to the one found in `/usr/bin/env` thus leading to nothing happening.   If you want to see what the shell spewed out when it failed your previous commands, you could try doing `non-working-command >> /home/USERNAME/CRAFTROOT/cron.log` to have it's output stored in a file to read later on.  

---

