Menu
Create Custom Side Menus
Frodo's Ghost | CakePHP and CronTab in Plesk
39
post-template-default,single,single-post,postid-39,single-format-standard,eltd-core-1.0.1,ajax_fade,page_not_loaded,,borderland - frodosghost-child-ver-1.0.0,borderland-ver-1.2, vertical_menu_with_scroll,smooth_scroll,side_menu_slide_with_content,width_470,paspartu_enabled,paspartu_on_bottom_fixed,wpb-js-composer js-comp-ver-4.4.4,vc_responsive

CakePHP and CronTab in Plesk

My latest little project has needed some automation to take it beyond me visiting a link everyday. I wanted to have a script that I had built in CakePHP just run at intervals, so that I wouldn’t have to manually run it. I was pointed in the direction of Cron a “… a time-based scheduling service in Unix-like computer operating systems” (link). Setting it up to run with CakePHP was rather simple, so lets see how.

I spent some time setting up a script in the controller to do what I needed for spamhaze. The controller Method checks a mailbox and returns the mail, cleans the email body, breaks up all the words and dumps them into a table, creating a tagged list of all the words appearing in the email.

Setting up the Cron job

Running a cron job is actually rather easy, it is just creating a string and calling it from the command line. But all that is something I didn’t have to worry about, because I use Plesk on the server – and it is just a matter of filling out a form and hitting submit. One thing you’ll need to remember is to use the UNIX Cron commands for the time variables.

The CronTab function in Plesk presents you with the timing fields and a command field (if you need to get your hands dirty in code-land visit Simple Sheel Script Back-Up). Using the UNIX time commands we can use the following numerals to be placed into the Plesk fields:

min     hour     day    month    weekday
0-59    0-23     1-31    1-12     0-6

These first five values are zero based and fit with their times they match. The weekday it runs on starts with 0 on Sunday. You can also use the select boxes provided in Plesk for the Month and the Day of the Week values.

One thing to remember is that the values represent at what time they will run, not with what specific frequency. Setting a value of 15 for the minutes will not run the cron every fifteen minutes, instead it will run it at 15 minutes past the hour. And having the hour set at 1 with not run the script every hour, but only the first hour which is 0100 hours.

If you’d like to use every value in a column, say every hour run your script, replace the value with an (*) asterisk.

So to run the in my setup, eventually I wanted it running at once every 10 minutes in every hour, everyday of the week, every month of the year. And it looked like this:

min     hour     day    month    weekday
*/10     *        *       *        */1

The last field in Plesk is the command field. This is the line that makes everything sweet, so we better get it right and not stuff it up like I did.

What Not to Do – Put a URL into the command line

Do not enter a web address in the command line thinking it’ll run because it is that clever. The command line is not that clever, in fact doing this proves that the operator of a computer can be stupid. Like Me. The command line is made to run unix commands and php scripts, not visit web addresses for you to run scripts.

What you can do, but shouldn’t – Excute a cake script by changing the index file in webroot

After I had tried and failed by putting the URL into the command line I found out that the command line could run a php script. So I thought I was going to pull down the whole Cake setup and program it all up in php. Ground up php coding seems a little old to me now, so I did some research.

The first result in google was this link. A way to call the Controller Method I had setup and running it like a php script. Just the way that the cron command would be expecting. And I implemented it and it worked.

But there were some doubts I had with it. Firstly, I didn’t want to leave a script that excecutes every ten minutes to a chance php ‘if‘ statement (may sound silly, but it did smell a bit like a hack). Secondly, the fact that the other sites that had mentioned using the Cron command this way had comments to use the Cake Shell. These comments were like whispers to a more subtle truth, and since these strange voices called, I followed.

How to Cron with CakePHP

One of the excellent features of Cake is the console. Up till now I have just used it in setting up the ACL gear when following the tutorial, and now that I have used it this I think it will be like a genius feature, a gift that keeps on giving.

If you need to access you controllers for a Cron task, look no further than here. In fact, any other way seems like a hack, because this isn’t even run via a php script, it is a command line interaction with you CakePHP install. Sounds like fun – even if it makes me sound ignorant of the deeper levels of code.

Cakes Console – Don’t Panic!

I don’t need to go into detail with running shells thorough the console, there is plenty of information if you need it. One thing, one major thing I should point out is not to be scared. Really, if you can craft a controller to execute your code, with a few changes you can get it running as a shell.

Most of the information you’ll need is found at The Book – The CakePHP Console. While some of it may not make sence, don’t panic. Seriously, the whole command, CLI, bash code is very bare, but via cake we can tame the beast. If you make sure you have “A command-line (CLI) build of PHP” we should be apples.

To check if the cake console ran on my server, I just pasted the code below into the CronTab command line and made myself a coffee while the cron script ran in the next 10 minutes. I got an email dumping out the ‘Welcome to CakePHP v1.2 Console’ script and we were good to go.
[bash]/var/www/vhosts/domain.com/httpdocs/cake/console/cake[/bash]

Controllers vs. Shells

Reading up in The Book it all seems easy to start: Creating Shells and Tasks. To start off I took the leap and copied all the controller code I had lovingly crafted *cough* into a new shell. Just create a file in app/vendors/shells/ and pasted it right in there.

Since the a new shell is like a controller just with a little different code:

class CheckShell extends Shell {
var $uses = array('Post','Tag');
var $tasks= array();
function main() {
}
}

The function Method() of the controller becomes the main() in the shell. You add models into the var $uses array, just like you shouldn’t in a controller.

Since most of the code in a shell is not to display text the way to get text to print – for example you’d like an email to let you know if there were any failures – is to use $this->out() to your shell.

If you’d like to share some smarts between shells, then set up a task, it is just an extension of a shell. After making a shell, just add it into the declares at the top.

And thats it. Leave all your Model saves and controller actions that were made to work in the controller. As long as the models are declared in the var $uses we should be good to go.

Calling a Shell from the Plesk Command Line

Back in Plesk now to the command line we left emptier earlier. Again this is easy, just three little things to put in. Again, an aside, there is no reference to a php script in the command line because we aren’t using php to initiate the shell code. Through the CakePHP Console we are

  1. First the direct path to the cake console – the cake/console/cake directory should be 750 for permissions.
  2. -app “direct or relative path to the app folder”
  3. Lastly the name of the shell you are calling.
direct Path to the cake folder                               relative or direct to app               shell
/var/www/vhosts/domain.com/httpdocs/cake/console/cake -app "/var/www/vhosts/domain.com/httpdocs/app" check

So even after changing a controller over to a shell it is nice and easy to call the job to run in cakephp. If I was not using Plesk, it would be easy too, just from the command line:

*/10 * * * */1 /var/www/vhosts/spamhaze.com/httpdocs/cake/console/cake -app "/var/www/vhosts/spamhaze.com/httpdocs/app" check

Summing Up

Whatever you would like to automate in your CakePHP application you can do it, and as long as you get the Model->Controller setup, or have even a simple grasp of CakePHP it’ll be easy to impliment a solution like this.

I think that is about all.

james
7 Comments
  • There was also this link as another way run the cron with CakePHP : http://www.usefulzero.com/2007/11/12/cron-job-in-cakephp-12/ : for informational sake.

    There is many ways to accomplish one task.

    June 1, 2009 at 4:53 am
  • I came across your blog entry on this topic as I am too in the process of setting up a cronjob to execute a cake shell I had created using DirectAdmin and wanted to confirm I was doing it right.

    My shell works fine from the command line adding a record into a table with no problems. However, when cron runs the shell, and I can see form my cron log on the server that the job is running, nothing happens .. any idea where I can find out more information on what is happening when the cronjob runs?

    July 8, 2009 at 4:24 pm
  • Yeah, I ran into the same issues playing with the Shell. Basically it comes down to resorting to debug() to sort out these issues.

    So, I would guess your first friend would be $this->out(); – Just place some variables in there and you shall see it in the log. With Plesk, you could send an email on the completion of every CRON, I found that time-consuming (waiting for the CRON to run) but rather effective.

    I also setup the method first in a controller, so I just had to massage off the little issues without the wait.

    Let me know how to go.

    July 10, 2009 at 2:36 am
  • info@shjansen.nl
    Reply

    Thanks for your article, it realy helps!

    Just a tip for all you devvers: 🙂

    Found a litte php script in the console folder called “cake.php”.

    You can use this to access the console without a CLI version of PHP.

    July 15, 2009 at 4:34 pm
  • ersaky@yahoo.com
    Reply

    thanks for your this useful article!

    October 7, 2010 at 11:16 am
  • qmvc.tel@gmail.com
    Reply

    thanks for all brother !

    August 11, 2012 at 11:52 pm
  • Shamim Ansari
    Reply

    Please tell me how to set it in controller function.

    August 6, 2013 at 1:56 am

Post a Comment