2

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?

1 Answer 1

1

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.

  1. The User running the cronjobs was different compared to the one you used in ssh => different permissions => somehow broke execution through cron.
  2. 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.
  3. 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.

4
  • 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
    Commented Dec 10, 2019 at 16:52
  • 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
    Commented Dec 11, 2019 at 7:35
  • • 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
    Commented Dec 11, 2019 at 19:06
  • 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
    Commented Dec 12, 2019 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.