Archive for the ‘CakePHP’ Category

Running Shells as Cronjobs with CakePHP on Ubuntu

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/example.com/public_html/app/Console/Command/ . 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/example.com/public_html/lib/Cake/Console:$PATH

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

Now type: cake -app /var/www/example.com/public_html/app 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.

Type crontab -e to open up the editor.

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

*/30 * * * * /var/www/example.com/public_html/lib/Cake/Console/cake -app “/var/www/example.com/public_html/app” hello

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

*/30 * * * * /var/www/example.com/public_html/lib/Cake/Console/cake -app “/var/www/example.com/public_html/app” hello –quiet >> /home/myusername/hello_cron.log

Hope this helps someone!

Easiest way to make a form in CakePHP

The easiest way to make the HTML for a simple form is to use CakePHP’s built-in FormHelper. Once you get your model and table set up with the fields you need, a little shortcut is to use the inputs() function so that you don’t have to type out each individual field input:

<?php
echo $this->Form->create('Post');
echo $this->Form->inputs();
echo $this->Form->end('Submit');
?>

This method puts Cake’s automagic form elements to work and will save some time and shave off a couple lines of code if your form isn’t too complicated.