Cron and Crontab

The Cron Daemon runs on Linux system and execute scheduled commands also known as cron jobs.

The Crontab is the program used to add, remove or list the tables used to drive the cron daemon. It creates user-specific cron jobs. It is also used to refer to the list of commands that you want to run on a regular schedule

System Crontab is a method of setting up system wide cron jobs. Here scripts are added to specific sub directories of /etc. This seem to be specific to REd Hat and CentOS


Cron Deamon

Cron is a daemon that runs on Linux that execute scheduled commands also known as cron jobs. Cron jobs may be scheduled in combination of the time, day of the month, month, day of the week, and week. Scheduled commands may be created by crontab command. Cron also reads files in /etc/ directories that start with cron* prefix.

For example, a cron job may be created to list and email all new files created in a specific directory.

The Cron daemon is started automatically from /etc/init.d on entering multi-user runlevels.


Command to check the cron deamon's status

> /sbin/service crond status
or
> service crond status

Crontab

The crontab command is used to add/edit crontab entries which are storeed in text files. These files will be stored in directory /var/spool/cron but these files are not supposed to edited directly. Instead use the crontab command to edit these files. Each user can have their own crontab. Crontab stands for "cron table," because it uses the job scheduler cron to execute tasks.

Command to open a crontab file which holds scheduling information.

# crontab -e

List your crontabs.

# crontab -l

Structure of crontab files

Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time.

  • MIN - Minutes
  • HOUR - Hours (24 time)
  • DOM - Day of the Month
  • MON - Month
  • DOW - Day of the week
  • COMMAND - The command you want to run. This can contain spaces or point to a bash script.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12".

A field may be an asterisk (*), which always stands for "first-last".


Example of a cron file entry. This entry will run list.sh file every minute of the day.

# MIN    HOUR    DOM     MON    DOW    COMMAND
   *      *       *       *      *     /home/USERNAME/list.sh

This entry will run list.sh every hour between 9AM and 5PM Monday to Friday.

# MIN    HOUR    DOM     MON    DOW    COMMAND   
   00   09-17     *       *     1-5    /home/USERNAME/list.sh   

After you set up the cron job, make sure you have created a shell program that corresponds to the program name called in your cron job. Here we create a program called list.sh in direxctory /home/USERNAME/, where USERNAME is user currently logged in.

ls -ails > /home/USERNAME/text.txt


To list current user's crontab entries

> crontab -l

To list user bob's crontab entries

> crontab -lu bob

To remove user bob's crontab entries

> crontab -ru bob

To view cron job log

>  cat /var/log/cron | more

To view messages log - some issues may show up here.

>  cat /var/log/messages | more


Cron Special Keywords

Instead of specifying values in the 5 fields, we can specify it using a single keyword as mentioned below.

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly, etc.. Table: Cron special keywords and its meaning

Keyword     Equivalent
@yearly     0 0 1 1 *
@monthly    0 0 1 * *
@daily      0 0 * * *
@hourly     0 * * * *
@reboot     Run at startup.

This entry will run list.sh every hour between 9AM and 5PM Monday to Friday.

@monthly   /home/USERNAME/list.sh   

Some more crontab examples:

run every minute of the day

* * * * * /home/USERNAME/list.sh 

run 8:30 10th day of 6th month every day

30 08 10 06 * /home/USERNAME/list.sh 

run 11:00 and 16:00 every day

00 11,16 * * * /home/USERNAME/list.sh 

run Every hour between 9:00 to 19:00

00 09-19 * * * /home/USERNAME/list.sh 

Monday(01) to Friday(05) 9:00 to 19:00

00 09-18 * * 1-5 /home/USERNAME/list.sh  

view my crontabs, or another user's crontabs

crontab -l
crontab -u OTHERUSER -l

System Crontab

There are also a series of directories under /etc. Any shell scripts added to these directories will be run at the frequency indicated by the directory name.


/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

Two special files allow you to list users who are allowed or not allowed to run cron jobs

/etc/cron.deny
/etc/cron.allow 

The /etc/crontab file sets up the cron environment. The first four lines are variables used to configure the environment in which the cron tasks are run. The SHELL variable tells the system which shell environment to use (in this example the bash shell), while the PATH variable defines the path used to execute commands. The output of the cron tasks are emailed to the username defined with the MAILTO variable. If the MAILTO variable is defined as an empty string (MAILTO=""), email is not sent. The HOME variable can be used to set the home directory to use when executing commands or scripts.

You can add contab instructions to this file. The setup system is similar to crontab command.

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# 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

To see cron jobs running in batch, use ps (current process) command

> ps aux | grep cron
root      2215  0.0  0.0 116896  1420 ?        Ss   Jan18   0:25 crond
root      2688  0.0  0.0 103248   844 pts/1    S+   21:27   0:00 grep cron