Running Shells as Cronjobs with CakePHP on Ubuntu

Share this post:

I found it a little tricky to get my CakePHP shell to run as a cronjob on my Ubuntu server, so I thought I'd do a little write up on how I ended up doing it. First off, I'm using Cakephp 2.0 and Ubuntu 10.04 LTS. I'm not a linux expert, which is probably why I had issues figuring it out in the first place.

The first step is to have a shell script you want to run. For the following example, I have a shell called HelloShell.php located in /var/www/ . See Cake's Console and Shells guide for more details on writing shell scripts.

I use the cake console pretty often for the bake functionality, so I always add a path to it so I can access from any directory.

To do this, I edited .profile in my home directory and added this line at the bottom to the location of my cake console:

export PATH=/var/www/$PATH  

I believe you have to logout and back in for it to take effect.

Now type:

cake -app /var/www/ hello  

If your path is set properly, you should see the output of your HelloShell.php script. Now we're good to go to setup the cronjob.


crontab -e  

to open up the editor.

Here are the settings I used to run my cronjob every 30 minutes:

*/30 *   *   *   * /var/www/ -app "/var/www/" hello**

You can also output to a log file with various levels of verbosity:

*/30 *   *   *   * /var/www/ -app "/var/www/" hello --quiet >> /home/myusername/hello_cron.log**

Hope this helps someone!


comments powered by Disqus