Managing Cron Jobs With PHP
In this tutorial, we’ll create a dynamic PHP class that, using a secure connection, provides us with a means to manipulate the cronTab!
An Overview of the Crontab
Let’s face it, having the ability to schedule tasks to run in the background is just great! From backing up an SQL database, fetching / sending emails to running clean up tasks, analyzing performance, or even grabbing RSS feeds, cron jobs are fantastic!
Although the syntax of scheduling a new job may seem daunting at first glance, it’s actually relatively simple to understand once you break it down. A cron job will always have five columns each of which represent a chronological operator followed by the full path and command to execute:
* * * * * home/path/to/command/the_command.sh
Each of the chronological columns has a specific relevance to the schedule of the task. They are as follows:
- Minutes represents the minutes of a given hour, 0-59 respectively.
- Hours represents the hours of a given day, 0-23 respectively.
- Days represents the days of a given month, 1-31 respectively.
- Months represents the months of a given year, 1-12 respectively.
- Day of the Week represents the day of the week, Sunday through Saturday, numerically, as 0-6 respectively.
Minutes [0-59] | Hours [0-23] | | Days [1-31] | | | Months [1-12] | | | | Days of the Week [Numeric, 0-6] | | | | | * * * * * home/path/to/command/the_command.sh
So, for example, if one wanted to schedule a task for 12am on the first day of every month it would look something like this:
0 0 1 * * home/path/to/command/the_command.sh
If we wanted to schedule a task to run every Saturday at 8:30am we’d write it as follows:
30 8 * * 6 home/path/to/command/the_command.sh
There are also a number of operators which can be used to customize the schedule even further:
- Commas is used to create a comma separated list of values for any of the cron columns.
- Dashes is used to specify a range of values.
- Asterisks is used to specify ‘all’ or ‘every’ value.
The cronTab, by default, will send an email notification whenever a scheduled task is executed.
The cronTab, by default, will send an email notification whenever a scheduled task is executed. In many circumstances, though, this just isn’t needed. We can easily suppress this functionality, though, by redirecting the standard output of this command to the ‘black hole’ or /dev/null device. Essentially, this is a file that will discard everything written to it. Output redirection is done via the > operator. Let’s take a look at how we can redirect standard output to the black hole using our sample cron job which runs a scheduled task every Saturday at 8:30am:
30 8 * * 6 home/path/to/command/the_command.sh >/dev/null
Additionally, if we’re redirecting the standard output to a the null device, we’ll probably want to redirect the standard errors as well. We can do this by simply redirecting standard errors to where the standard output is already redirected, the null device!
30 8 * * 6 home/path/to/command/the_command.sh >/dev/null 2>&1
In order to manage the cronTab with PHP, we’ll need the ability to execute commands, on the remote server, as the user whose cronTab we’re editing. Fortunately, PHP provides us with a simple way to do this via the SSH2 library. You may or may not have this library installed so if you don’t, you’ll want to get it installed: