Cron is utility for Unix/Linux that allows you to run scripts/programs at a specified time/date automatically. It is mainly used by system administrators who need to run daily/weekly backups but can often be needed by the standard user. Cron is a daemon and is usually included and started with most distributions.
This tutorial is focused on Fedora, but may apply to other Linux distributions, it is assumed that you have root access to your computer and a basic knowledge of text editing and general use of Linux.
Crond is the name of the cron daemon that must be running in order to use cron. So before we continue, the first thing to do is check whether cron has already been started. You can do this by issuing the command:
ps aux | grep crond
You should get two lines returned, similar to those shown below.
[savage@savnet-wrk-01 ~]$ ps aux | grep crond root 1948 0.0 0.2 4544 1164 ? Ss 11:56 0:00 crond savage 4890 0.0 0.1 3768 680 pts/3 R+ 17:43 0:00 grep crond
The top line is the cron daemon, the bottom line is the ps command we just executed. If you only get the bottom line, you'll need to issue the command (as root) 'service crond start'.
As is common with Linux, there are various configuration files associated with cron. Within /etc/ cron has five main ways to schedule tasks to execute.
Within /etc/ are four directories, cron.daily, cron.hourly, cron.monthly and cron.weekly. The purpose of these directories is fairly obvious, any scripts placed within the directories are executed depending on the directory you place them in.
Just to be clear, if you want a script to run every day, you would place it in the cron.daily directory.
Remember to make sure the scripts are executable, otherwise they won't run.
While the above method is fine and fairly simple, it doesn't provide a large amount of control over when commands run. Below is a copy of the crontab file taken from this server.
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 00 3 * * * root /root/scripts/backup --silent 30 3 * * * root /root/scripts/webalizer 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
The first part sets the variables used by cron. You shouldn't need to amend these, but definitions of what each does are below just in case.
SHELL specifies what shell cron should run under, if it is not specified here, it will default to the shell specified in the /etc/passwd file.
PATH specifies the directories that cron will look in to find the scripts. If the script is not in one of the directories specified by PATH, then you will have to provide the full path to the script, otherwise cron will be unable to find it.
MAILTO is who gets mailed the output from each command. Cron will e-mail the output from any commands executed, this includes errors, reports etc. If this is left blank, the output will be mailed to the owner of the process that created the output.
HOME is the home directory used by cron, if left blank this will default to the entry in /etc/passwd.
The 'run-parts' section contains the actual schedule of commands the cron will run. You should have a line for each different command/script. The format of each line is shown below:
minute hour dom month dow user command
minute, hour, month: Specifies the minute, hour and month to run the command on, please note although I have written these three together, the month value goes on the 4th column, not the 3rd.
dom: The Day of Month you want the command to run on. If you wanted a command to run on the 1st of each month you would add the line: '01 * 1 * * root command'.
dow: Day of Week to run the command, this can be text based (sun, mon etc) or numeric (0-7). If you wanted a command to run every Monday, you would add the line '01 * * * 1 root command'. Both 0 and 7 are Sunday.
user: This specifies the user that cron will run the command as.
command: This is the command, if the command (executable or script) is in a directory specified in the PATH variable, you need only give the filename, otherwise you will need to supply the entire path to the command.
Cron allows even more control of when a command is executed.
You can supply lists in each of the fields in the format 1,2,3 or 1-3. For example '01 12 1,5,10 * * root /command' will run the command on the 1st, 5th and 10th day of the month.
You can also skip days, for example a value of */2 in the hour field would make the command run every 2 hours.
Man pages: cron(8) crontab(5) crontab(1)