Running PHP cron jobs – regular scheduled tasks in PHP


Scheduled tasks are a fairly common feature in modern web applications. From cleaning out caches every 24 hours to checking subscription periods and even generating reports, more web applications live by the clock than ever before. But how do we schedule the execution of a PHP script on the server side?

The answer, for Linux-based servers, is crontab. The crontab utility on UNIX systems allows commands to be executed and predefined intervals. These commands are essentially the same commands as you would run from any local console. For example, you could create a new cron “job” to run a bash script, and set it to run once per day at 12:00 midnight.

 

In terms of PHP, we can then call our PHP scripts via command line using the `php` binary. There are two issues here: first, we need to know where PHP is, and second, we need to know where our scripts are. Say your scripts are in /home/username/example.com, and your PHP binary is in /usr/bin, Your command to run your script could be this:

/usr/bin/php /home/username/example.com/myscript.php

Now, we have to configure cron to execute this. If your host has cPanel, you may have a cron job interface which will take this command as an option and allow you to configure visually the times at which it will execute. If you don’t have some kind of web-based interface, you’ll need to go one step further and manually create crontab entries via a command line terminal.

Fire up a console window at your server (e.g. in PuTTY) and run “crontab -e”. If you are prompted for an email address, enter an address you can easily manage e.g. a gmail inbox or a catch-all. A command-line text editor such as nano or vi/emacs should appear, and you’re ready to start creating crontab entries.

Here’s a sample entry:

0 0 * * * /usr/bin/php /home/username/myscript.php

The first section is the key: this defines the timing. There are five fields, minute, hour, day of month, month and day of week. Each takes a numerical value (typically starting at 0, not 1) or an asterisk to represent “all values”. Ranges are also an option, e.g. 1-5. For example, here we want the 0th minute of the 0th hour of every day of month, every month, on every day of week – in otherwords, midnight every day. This page has a great tutorial on cron scheduling.

Once you’ve worked out the cron entry for your intended timing, just add the entry to your crontab file – edited by running `crontab -e` – and save and exit. Crontab will automatically manage the changes and start scheduling your tasks immediately. If your scripts have any output, you will also receive an email with the output. Practise this a little, and within no time you’ll be a PHP task scheduling master!

Source : http://www.developertutorials.com

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s