Setting up cron jobs with Cake shells
This post will primarily be about the process I had to go through to get cron jobs and shells working on Dreamhost. The main problem I kept running into was that the shells would simply not work at all through a cron job; yet they worked when I manually did it through SSH. The weird thing was the source of the console/cake file was being printed in my logs, instead of executed. Below is a quick checklist of things to look out for:
TERM environment variable not set
This isn't really an error, but the TERM variable is used by the server environment and does not affect the shell, even if the error is thrown. Now I may not know much about server maintenance and configuration, but adding the following code to console/cake seemed to do the trick (Hats off to Matt Curry for the tip). And of course, you should change linux to whatever your server is.
TERM=linux
export TERM
Cron environment not PHP 5
This was a weird problem I ran into. It seemed the environment in which my crons ran was PHP 4, so I was receiving unexpected results and would receive this error (it mainly depends where the php folder is located and if its loaded correctly).
console/cake: line 30: exec: php: not found
To fix this, I had to rewrite the cake console file and update it with the path to the PHP folder (path should represent your servers structure). You should only need to do this if you can't upgrade PHP to 5 for some reason.
exec php -q ${LIB}cake.php -working "${APP}" "$@"
# change to
exec /usr/local/php5/bin/php -q ${LIB}cake.php -working "${APP}" "$@"
Making sure your cron has ownership
A simple problem with a simple fix. Since the user that uploaded the console files was different than the user running the cron, I would receive this error:
sh: cake/console/cake: Permission denied
All I had to do was switch the cron user to the user that owns the file, and it fixed the permissions problem.
Making sure the cake file uses unix line endings
This was another pain in the ass that took me forever to get working. My cron environment would throw this error once I fixed the things above (this applies to my Dreamhost server and may not apply to all).
sh: cake/console/cake: /bin/bash^M: bad interpreter: No such file or directory
It required me contacting my host and asking for help on this one. I tried saving all my files as unix and made sure my FTP was not converting them in any way. Still nothing. So my host told me about this SSH command called dos2unix which would convert the file to unix line endings, and wallah, magic! Everything seemed to be working now.
dos2unix cake/console/cake
These are just a few of the minor setbacks I had to deal with when setting up my cron jobs. Most of this really applies to my Dreamhost server, as my Media Temple server worked right away with no configuration. Furthermore, here are some more Dreamhost articles that I used for this problem, that might be of aid:
Editing Your Environment Profile
Make PHP5 the Default in the Shell
9 Comments
Your last tip "dos2unix" has saved me !
My project leader chose to go with HostGator instead of DreamHost after I got it working. As you know, each hosting service is a little different. Here is what I had to do to execute a shell task:
php /home/USER NAME/public_html/cake/console/cake.php -app /home/USER NAME/public_html/cake/app reminders lof
After getting access to the "GET" command and "SSH" from HostGator, the command executes but yields this error message:
Error: Missing Database Connection. Try 'cake bake'
X-Powered-By: PHP/5.2.13
Content-type: text/html
This works beautifully on DreamHost. Do you have any idea why this would come up for me? Thanks in advance.
I have been trying to send e-mails from the cron job using a Cake shell. Turns out you need to use a Cake shell task too. If you ever need to do that, this article has excellent examples on how to e-mail from a shell:
http://bakery.cakephp.org/articles/view/updated-swiftmailer-4-xx-component-with-attachments-and-plugins
Your article and the above article were just what I needed. Thanks again!
Which cake file is this? I've looked at the following files:
../app/cake/console/cake.bat
../app/cake/console/cake.php
../app/cake/console/error.php
I can't find where to make the change. The ../app/cake/console/cake file is an application that launches Terminal in my case.
Would you please point me in the right direction for the file? Thank you in advance for the help and for the great article!
Which cake file is this? I've looked at the following files:
../app/cake/console/cake.bat
../app/cake/console/cake.php
../app/cake/console/error.php
I can't find where to make the change. The ../app/cake/console/cake file is an application that launches Terminal in my case.
Would you please point me in the right direction for the file? Thank you in advance for the help and for the great article!
It took me quite awhile to figure out the line endings issue too, but once I did, it was a simple switch in my text editor (yay text-editor power!) to only use line-feeds as the newline.
I didn't have the permission issue that you did (that I recall), but either way, permission denied errors are pretty easy to figure out a solution for.
Okay, so now for the really ugly line of code for my to run my own cake console shell in cron (on dreamhost):
/usr/local/php5/bin/php /home/USER_NAME/cake/console/cake.php -app /home/USER_NAME/cake_apps/app_01 prowl load_alert help
...where the -app parameter is my full path to the app folder...then just call the appropriate shell command. It's much more ugly, but rather than configure my environment specifically for CakePHP, I can continue to use it for other things as well.