PHP manage multiple cronjobs with a single crontab entry

By | October 23, 2011

In many php applications there are multiple tasks that need to be run via cron at different times. In a typical application you may be doing the following tasks via cronjobs :

1. Backup database.
2. Send out email to subscribers.
3. Clear temporary files.
4. Fetch xml feeds from some source and store them in database.

So if you had separate php files doing these tasks , and had a cronjob entry for each , your cronjob could look like this :

MAILTO="[email protected]"
0 0 * * * /var/www/my_app/backup_database.php
12 2 * * * /var/www/my_app/email_to_subscribers.php
10 5 * * * /var/www/my_app/clear_temp_files.php
10 7 * * * /var/www/my_app/fetch_xml_feeds.php

The above is the simplest approach to manage multiple cronjobs for your php application. However this approach has many drawbacks :

1. Multiple entries in crontabs means more time and effort needed to edit the crontab and maintain it.

- Since this approach involves writing each task separately in the cron list , it becomes difficult to maintain.

2. The crontab command has to be used everytime a change is to be made.

- You need to either manually do a `crontab -t` in the shell prompt or make your php application do it , everytime there is a change in either the tasks or their timing.

3. If the script name changes then have to edit the crontab file again.
4. A log email would be generated for every cron run, creating multiple log emails.

- Every task that is running would generate a cronlog and email itself to the email specified

5. Inefficient when there are 10s or 100s of tasks to be run via cron.

- Do you think it is a good idea if you had many many tasks to do.

An alternative solution

How about having only 1 entry in the cronjobs, and that 1 cronjob manages the rest of the cronjobs.

1. Add only 1 task to the crontab rule say :

* * * * * php /path/to/cronjob.php

The cronjob.php file would run all the tasks that need to be run via cron. Its important to note that this cronjob.php will run every minute and forever. Do not be worried about it eating too much of system resources. It is a faily light thing and does not load your CPU or RAM with anything heavy.

Now the next thing would be to make sure cronjob.php can run all the tasks at the right time.

2. Now we need to have different tasks have a different cron schedule. Lets say there are 3 different tasks to run at 3 different times :

0 5 * * * database_backup
0 5 1,15 * * monthly_sales_report
0 10 15 02 * purchase_report

The cronjob.php that runs every minute should have an array like this :

$cronjobs = array();

$cronjobs['database_backup'] = '0 5 * * *';
$cronjobs['monthly_sales_report'] = '0 5 1,15 * *';
$cronjobs['purchase_report'] = '0 10 15 02 *';

Now we test each job/task for the timestamp and run it like this :

foreach($cronjobs as $method => $cron)
{
	$time = time();
        if( is_time_cron($time , $cron) )
	{
		$result = $method();
		echo $result;
	}
}

is_time_cron checks if the current timestamp matches the cron schedule or not. If it matches , then the task is executed. The is_time_cron method can be found in the previous post here.

In this approach the benefits are :

1. Only 1 crontab entry.

The crontab list is clean and your application does not overload it.

2. Easy to maintain , does not need any modification unless the script path/name changes.

The crontab once created does not need any change unless the name or path of 'cronjob.php' changes. Meanwhile the jobs inside cronjob.php can change their names , schedules and anything very easily.

3. To add/edit/remove tasks or change their time schedules only the cronjob.php needs to be changed.

This means easier modification , maintenance and the application can provide simple user interface to make changes anytime without the need to use crontab commands or anything as such.

The above mentioned approach can be applied to any language , not just php.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

7 Comments

PHP manage multiple cronjobs with a single crontab entry
  1. Aldis

    Great!
    My hoster allows only one cron job so all tasks are executed in one php and all at once.
    This info will help me a lot! Thanks! :)

  2. ikke

    Yeah, right…
    And where do you store the command to be executed, huh?
    That needs another function because this only works with bogus commands like “database_backup”.
    You still need to define “database_backup” somewhere else.

    And what about a complicated command like this one:
    /usr/bin/find /home/user/Procmail -name pmlog* -type f -mtime +60 -exec /bin/rm -f {} ;

    That´s not going to work ever.

    To be short this is another example of doing things complicated while it can be done simple.

    1. Binary Tides Post author

      “And where do you store the command to be executed, huh?
      That needs another function because this only works with bogus commands like “database_backup”.”

      — Every function/command needs to be stored somewhere in the application code.

      “You still need to define “database_backup” somewhere else.”

      — Ofcourse , any tasks needs a definition.


      And what about a complicated command like this one:
      /usr/bin/find /home/user/Procmail -name pmlog* -type f -mtime +60 -exec /bin/rm -f {} ;

      That´s not going to work ever.

      — Not going to work means ? The whole purpose is to manage task scheduling , not what the tasks do or how they do.
      For a command of that kind , you can put it in a php function and call system , exec , passthru or shell_exec.

      “To be short this is another example of doing things complicated while it can be done simple.”

      — Its just about managing the task automation. How the task is done is upto the user or the application.

Leave a Reply

Your email address will not be published. Required fields are marked *