1. minute (0-59)
  2. hour of the day (0-23)
  3. day of month (1-31)
  4. month (1-12)
  5. day of week (0-7) where 0 and 7 are sunday

What is the meaning of the asterisk?

The asterisk (*) represents all possible numbers for that position. For example, asterisk in the minute position would make it run every minute.

How can we schedule a task to run at the 10th minute of the hour?

10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1

How can we schedule a task to run once every 10 minute interval?

*/10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1

How can we schedule a task to run every minute?

* * * * * [command]    # Runs every minute

How can we schedule a task to run once every hour at minute zero?

0 * * * * [command]    # Runs at minute zero, every hour

How can we schedule a task to run at 2:30am every day?

30 2 * * * [command]  # Runs at 2:30am every day

How can we schedule a task to run once a month, on the second day of the month at midnight?

0 0 2 * * [command]

How can we schedule a task to run on Mondays, every hour?

0 * * * 1 [command]

How can we schedule a task to run every 15 minute interval?

0,15,30,45 * * * * [command]
*/15 * * * * [command]
0,10,20 * * * * [command] # You can use multiple numbers separated by commas. This will run three times every hour, at minutes 0, 10 and 20
*/5 * * * * [command]  # Division operator is also used. This will run 12 times per hour, i.e. every 5 minutes
0 5-10 * * * [command] # Dash can be used to specify a range. This will run once every hour between 5:00am and 10:00am
@reboot [command] # There is a special keyword that will let you run a cron job every time the server is rebooted
crontab -e  # Edit the contents of the crontab
crontab -l   # List the contents of the crontab
crontab -r  # Delete the contents of the crontab

You can add comments followed by the # character.

By default the output from the crons get sent via e-mail, unless you discard them or redirect them to a file. The MAILTO setting let's you set or change which e-mail address to send them to:

# This cron job does something very important
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1

If you do not handle the output of the cron script, it will send them as e-mails to your user account on the server. If you put "> /dev/null 2>&1" at the end of the cron job command (or any command), the output will be discarded.

How can we prevent Cron Job Collision?

In some cases you may have frequent running cron jobs, and you may not want them to collide if they take longer to run than the frequency itself. For example, you may have a cron job running every minute. Yet, every once in a while it may take longer than one minute to run. This can cause another instance of the same cron script to start running before the previous one finishes. You can create too many busy processes this way and possibly crash the server if they keep slowing down each other, and cause even more processes to be created over time. This problem can be addressed via file locking, and more specifically the non-blocking (LOCK_NB) type of file locks. You can add this code to the cron job script:

$fp = fopen('/tmp/lock.txt', 'r+');
if(!flock($fp, LOCK_EX | LOCK_NB)) {
    echo 'Unable to obtain lock';
/* ... */

With regular file locks the flock() function call would block the script if there is an existing lock. And it would release once that lock is gone. However, with a non-blocking lock, such as in the code above, the function call does not stop the script, but it immediately returns FALSE if there is an existing lock. So in this case, we can immediately exit the script when we see there is an existing lock, which indicates that another cron job is currently running.

What is the purpose of the crontab command?

It allows users to edit crontab entries (in other words, use cron to run jobs).

How can we limit access to the crontab command?

The file cron.allow and cron.deny control access to the crontab command. Both file contains a list of usernames, one per line. If cron.allow exists, then usernames must be in it in order to run crontab. If cron.deny exist, then any user not listed in cron.deny may use crontab. If neither file exist, only root may use cron.

How can we use crontab to manipulate cron entries?

crontab -e

The above command create an editor session. Basically, it will launch vi (or perhaps whatever your preferred editor is). The format of each cron entry:

minutes, hours, day_of_month, month, week_day, command

When we are done making changes, just save the file and quit the editor.

How can we make a back up of our cron entries?

crontab -l > mycron.txt

In the above command, the option is the lower-case letter L. The above command write existing cron entries to standard out which we redirect to a file mycron.txt.

How can we install new cron entries from a file?

If we have a file containing all the cron entries that we want, we can install these cron entries from this file:

crontab filename

Where is the configuration file for cron?


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License