Schedule crons using crontab

Many times we need to have some process to run automatically at set times or after set intervals. This can be easily done in Linux/Unix by using cron jobs. In this post I’ll talk cron and crontabs.

Topics Covered:

What is cron?

Cron is an Unix utility which allows you to automatically run tasks periodically at set times or set time intervals. Cron can be used to schedule tasks as recurring or even one-time. It is usually used to perform system maintainance, administration, etc. but can also be used for other activities like sending emails at set intervals, etc.

What is crontab?

crontab is a file which contains the details of which task has to be run when. It is essentially a schedule of all crons. Each user can have their own crontab, which are located in /var/spool/, but should not be edited directly. They should be edited only by the crontab -e command.

Crontab Restrictions

crontab uses the files cron.allow and cron.deny to control who has the access to crontabs.
cron.allow – If this exists then the user must be present in it to be able to execute the crontab command.
cron.deny – If the cron.allow file does not exists and cron.deny file exists, then only the users not listed in the cron.deny file will be able to execute crontabs.
If both cron.allow and cron.deny files don’t exist, then only the super user will be allowed to executed crontabs.

Some Crontab commands

crontab -e Edit the user’s crontab file. If a file does not exists it will be created
crontab -l List/Display the user’s crontab file
crontab -r Remove the user’s crontab file
crontab -u Specify the name of the user whose crontab file has to be tweaked. If this option is not present then crontab will use the current user’s file.

Crontab file

A crontab file has five fields for specifying when the command should be executed followed by the command which is to be executed.

*	*	*	*	*	command to be executed
-	-	-	-	-
|	|	|	|	|
|	|	|	|	+----- day of week (0 - 6) (Sunday=0)
|	|	|	+------- month (1 - 12)
|	|	+--------- day of month (1 - 31)
|	+----------- hour (0 - 23)
+------------- min (0 - 59)

How to setup cron in crontab?

To setup cron, edit the crontab file using “crontab -e” command. For each task that is to be executed add a line in the above format. Say you want to schedule 2 crons, 1 to run daily clean up tasks and 1 to run hourly cleanup tasks. To do this you will add the following 2 lines to your crontab file.

0     0     *     *     *         /path/to/daily/cleanup/cronfile.php
0     *     *     *     *         /path/to/hourly/cleanup/file.php

So these two lines schedules the files “/path/to/daily/cleanup/cronfile.php” to run everyday at midnight and “/path/to/hour/cleanup/file.php” every hour respectively. You can schedule any file or command to be executed. You can execute a shell script and a wget command using crontab entries similar to following:

# run a shell script at 2:00am
0 2 * * * /path/to/shell/script.sh
# this command will run at 12:30, 1:30, etc.
30 * * * * /usr/bin/wget -O - -q -t 1 http://localhost/cron.php

Disable Email

Every time a cron job is executed an email is sent to the user account executing it. If we do not want to send out emails than we can add the following command at the end of the cron job line.

>/dev/null 2>&1

eg.

0     *     *     *     *         /path/to/cron >/dev/null 2>&1

Example crontab entries

min hour day of month month day of week Execution Time
* * * * * Every minute. This will run at 12:00, 12:01, 12:02, etc
*/2 * * * * Once every 2 minutes. This will run at 12:00, 12:02, 12:04, etc
*/5 * * * * Once every 5 minutes. This will run at 12:00, 12:05, 12:10, etc
*/10 * * * * Once every 10 minutes. This will run at 12:00, 12:10, 12:20, etc
0,10,20,30,40,50 * * * * Once every 10 minutes. This will run at 12:00, 12:10, 12:20, etc
0 * * * * Once an hour. This will run at 12:00, 1:00, etc
15 * * * * At 15 minutes past an hour. This will run at 12:15, 1:15, etc
0 0 * * * Once a day. This will run at 12:00 (midnight)
30 0 * * * Everyday at specified time. This will run at 12:30 am
30 0 * * 1 Once a week. This will run the cron at 12:30am every Monday
30 22 * * 1-5 On weekdays. This will run at 10:30pm every weekday (Mon-Fri)
30 22 * * 1,2,3,4,5 On weekdays. This will run at 10:30pm every weekday (Mon-Fri)
0 0,2,6,12,20,23 * * * Specific hours. This will run at 12am, 2am, 6am, 12 noon, 8pm and 11pm everyday
0 0 1 1 * Once a year. This will run at 12am (midnight) on 1st Jan
30 0 1,10,20 * * Specific dates. This will run at 12:30 am on 1st, 10th and 20th of every month
0 12 * 6 1-5 Weekdays in specific month. This will run at 12 noon every weekday (Mon-Fri) only in June.
0 0 1 1,6 * Once in 6 month. This will run at midnight on 1st Jan and 1st June

Related posts:

  1. .htaccess tips
  2. How to generate passwords for .htpasswd using PHP
  3. Migrating servers using DNS TTL for minimum downtime
  4. more .htaccess tips

2 thoughts on “Schedule crons using crontab”

Leave a Reply