Cron is the system process that will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule. Crontab in Linux is the program used to edit, remove or list the tables used to drive the cron daemon. cron jobs can be allowed or disallowed for individual users, as specified in the files cron.allow and cron.deny, located in the directory /etc. If the cron.allow file exists, a user must be listed there in order to be allowed to use a given command. If the cron.allow file does not exist but the cron.deny file does, then a user must not be listed there in order to use a given command. If neither of these files exists, only the superuser will be allowed to use a given command.
Syntax:
crontab [-u user] [-l | -r | -e] [-i] [-s]
Example:
Let’s say you have a script that backs up important files, or creates a report about system statistics, for example. Let’s say the script is called /tmp/linuxinanutshell/scripts/do-every-day.sh, and you want to run it every morning at 5 A.M.
To edit the crontab, use this command:
crontab -e
This will open the crontab in a text editor (Usually this is vi or vim, but it may be something else depending on your Linux distribution). Save the below entry in opened file:
0 5 * * * /tmp/linuxinanutshell/scripts/do-every-day.sh
Cron Command Format
Each cron command in the crontab file has five time and date fields, followed by a user name if it is the system crontab file, followed by a command. Commands are executed by cron when the minute, hour, and month of year fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current time.
Field | Allowed values |
minute | 0-59 |
hour | 0-23 |
day of month | 1-31 |
month | 1-12 (or names) |
day of week | 0-7 (0 or 7 is Sun, or use names) |
Refer other examples
#This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m
00 09-18 * * * /tmp/linuxinanutshell/scripts/db-status
#This example will check the disk space every 10 minutes.
*/10 * * * * /tmp/linuxinanutshell/scripts/disk-check.sh
#This example will execute the shell script tape-backup at 00:00 on 1st of every month.
@monthly /tmp/linuxinanutshell/scripts/backup.sh