Cron is a daemon on linux and unix based operating systems.
Cron is a job scheduler which we can be used to configure jobs or run commands at a specific time in a day, month or year.
Cron is quite configurable enabling us to run jobs and commands periodically, at fixed times or dates and also in intervals. This is very useful for system administration or system maintenance automation. Cron is configured via crontab files. These are files on filesystem which hold line by line configurations of cron jobs. Each line in crontab file is a job scheduled to run at a specific time.
Each operating system user uses it’s own crontab file and can schedule jobs independently. These crontab files are stored in “/var/spool/cron” directory in a file with the users username.
Please note that user crontab files do not exist until the user has scheduled it’s first job via cron.
There is also a globally defined crontab file which can hold scheduled jobs from all of the operating system users – “/etc/crontab“
Start and Stop Cron Daemon
We can stop, start or restart Cron daemon as usual via init script. The Cron daemon on linux is defined by a Cron Daemon (crond).
Check if Cron is running:
[root@geekpeek ~]# /etc/init.d/crond status crond is stopped
Start Cron:
[root@geekpeek ~]# /etc/init.d/crond start Starting crond: [ OK ]
Stop Cron:
[root@geekpeek ~]# /etc/init.d/crond stop Stopping crond: [ OK ]
Crontab Files
We already explained that there are more than one crontab file on the filesystem. User specific crontab files are located in “/var/spool/cron” directory in a file with the users username and the global crontab file is located at “/etc/crontab“.
Global crontab file must be edited with your favorite console text editor (usually vi or vim) to schedule a new job and user crontab files are edited with “crontab” console command.
Global Crontab File – /etc/crontab
As already said, editing “/etc/crontab“ file is done with your favorite console text editor.
When editing global crontab file on CentOS or Red Hat (not sure about other distros), good thing is that the syntax is explained at the first few lines of the file:
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
- Let’s configure a “/home/mitch/test.sh” script to run under user “mitch” every day at “00:01h”. We need to add the following line to /etc/crontab:
01 0 * * * mitch /home/mitch/test.sh
It’s quite simple, right?
“01” are the minutes, “02” is the hour, followed by three “*” which instructs this job to run every day of month, month and day of week as user “mitch” followed by path to the script/command to run “/home/mitch/test.sh”.
- Let’s configure a “/usr/sbin/time-check.sh” script to run under user “root” every “16th minute” of the hour:
*/16 * * * * root /usr/sbin/time-check.sh
We can also use wildcards like “*”, “,”, “/” or “-“ in crontab files. The “*/16” instructs Cron daemon to run this job every “16th minute”, and the followed “*” makes this job execute every hour, day of month, month and day of week as user root followed by the script to run /usr/sbin/time-check.sh.
- We can also schedule a job to run a script “/usr/sbin/december.sh” only on the “15th, 16th 17th and 18th” of December at “13:00h”, and run it as user “sarah”.
We can achieve this with the following line:
00 13 15-18 12 * sarah /usr/sbin/december.sh
As we can see we used wildcard “-“ to cover “15th, 16th 17th and 18th” day of the month. We could achieve the same using the “,” wildcard and writing it down like this “15,16,17,18”.
- Another interesting example would be to run the script “/usr/sbin/sat-sun.sh” every “Saturday and Sunday” at “03:00h” as user “root”:
00 03 * * 7,0 root /usr/sbin/sat-sun.sh
Voila, i think we covered most of the use-cases when scheduling a new job via Cron daemon.
User Crontab Files
User specific crontab files are located in “/var/spool/cron” directory in a file with the users username. Please note that user crontab files do not exist until the user has scheduled it’s first job via cron.
The syntax when scheduling a job via user crontab files is the same as with global crontab file except we leave out the “username” to run as definition since this information is already known.
We can edit user crontab files with:
- Console text editor
- “crontab” console command
There are not many options to use with “crontab” console command:
usage: crontab [-u user] file crontab [-u user] [ -e | -l | -r ] (default operation is replace, per 1003.2) -e (edit user's crontab) -l (list user's crontab) -r (delete user's crontab) -i (prompt before deleting user's crontab) -s (selinux context)
Display user “mitch” scheduled jobs with “crontab” console command (must have root privileges):
[root@geekpeek1 ~]# crontab -u mitch -l 01 0 * * * /home/mitch/test.sh
Edit user “mitch” scheduled jobs (must have root privileges):
[root@geekpeek1 ~]# crontab -u mitch -e
After running this command, “vi” editor opens up and enables us to add, change or delete scheduled jobs for user “mitch”. When saving and quiting (:wq) scheduled jobs are saved to /var/spool/cron/mitch file.
If you do not have root privileges you will only be able to edit your own crontab file.
List my scheduled cron jobs:
[sarah@geekpeek1 ~]$ crontab -l 00 13 15-18 12 * /usr/sbin/december.sh
Edit my cron jobs:
[sarah@geekpeek1 ~]$ crontab -e
Cron Log File
Cron log file is located at “/var/log/cron” and it holds all of the cron related information from daemon startup, to executed cron jobs and access to user crontab files.
Startup of Cron daemon:
Nov 26 14:04:54 geekpeek crond[2727]: (CRON) STARTUP (1.4.4) Nov 26 14:04:54 geekpeek crond[2727]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 98% if used.) Nov 26 14:04:55 geekpeek crond[2727]: (CRON) INFO (running with inotify support) Nov 26 14:04:55 geekpeek crond[2727]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
User “sarah” editing crontab file and reload of scheduled jobs:
Nov 26 14:06:43 geekpeek crontab[2741]: (sarah) BEGIN EDIT (sarah) Nov 26 14:06:50 geekpeek crontab[2741]: (sarah) REPLACE (sarah) Nov 26 14:06:50 geekpeek crontab[2741]: (sarah) END EDIT (sarah) Nov 26 14:07:01 geekpeek crond[2727]: (sarah) RELOAD (/var/spool/cron/sarah)
User “sarah” running a scheduled job:
Nov 26 14:07:02 geekpeek CROND[2750]: (sarah) CMD (/home/sarah/test.sh)