Cron job is used to schedule commands that need to be executed periodically. Using it, you can configure certain commands or scripts to run periodically within a set time. cron is one of the most useful tools in Linux or Unix-like systems. The cron service (daemon) runs in the background of the system and continuously checks the /etc/crontab file and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.
crontab command
crontab is a command used to install, uninstall or list scheduled tasks. The cron configuration file is used to drive Vixie Cron's cron(8) daemon. Each user can have their own crontab file. Although these files are located in the /var/spool/cron/crontabs directory, it does not mean that you can edit them directly. You need to edit or configure your own scheduled tasks through the crontab command.
Types of scheduled configuration files
Configuration files are divided into the following different types:
System-level crontab for UNIX or Linux: This type is usually used by system services and important tasks that require root or similar permissions. The sixth field (see field introduction below) is the user name, which is used to specify the user identity as which this command is executed. In this way, the system's crontab can perform operations as any user.
User's crontab: Users can use the crontab command to install their own scheduled tasks. The sixth field is the command that needs to be run. All commands will be run as the user who created the crontab task.
Note: This Q&A Cron implementation was written by Paul Vixie and is included in many Linux distributions and Unix-like systems (such as the popular 4th version of BSD). Its syntax is compatible with various crond implementations.
So how do I install, create or edit my own scheduled tasks?
To edit your crontab file, you need to type the following command at the Linux or Unix shell prompt:
$ crontab -e
crontab syntax (field introduction)
The syntax is:
1 2 3 4 5 /path/to/command arg1 arg2
or
1 2 3 4 5 /root/ntp_sync.sh
Among them:
The first field: minutes (0-59)
The second field: Hour (0-23)
The 3rd field: Date (0-31)
The 4th field: Month (0-12 [12 represents December])
The 5th field: Day of the week ( 0-7 [7 or 0 represents Sunday])
/path/to/command – The name of the script or command planned to be executed
Easy to remember format:
* * * * * Command to be executed
--- -------------| | ----- Month (1 - 12)| | -------- Day of January (1 - 31)| ---------- Hour (0 - 23)
------------ Minutes (0 - 59)
Simple crontab example:
### Run backupscript script every 5 minutes ##*/5 * * * * /root /backupscript.sh### Run the backupscript script at 1 am every day ##0 1 * * * /root/backupscript.sh### Run the backupscript script at 3:15 am on the first morning of every month ##15 3 1 * * /root/backupscript.sh
How to use operators
Operators allow you to specify multiple values for a field. There are three operators available:
Star (*): This operator is a field Specify all available values. For example, in an hour field, an asterisk corresponds to each hour; in a month field, an asterisk corresponds to each month.
Comma (,): This operator specifies a list containing multiple values, for example: 1,5,10,15,20,25.
Hash (-): This operator specifies a value A range, for example: 5-15, is equivalent to typing 5,6,7,8,9,…,13,14,15 using the comma operator.
Separator (/): This operator specifies a step value, for example: 0-23/ can be used in the hour field to specify that a certain command is executed every hour. The step value can also be followed by the asterisk operator. If you want the command line to be executed every 2 hours, you can use */2.
How to disable email output
By default, the output of a command or script (if any) will be sent to your local email account. If you want to stop receiving emails sent by crontab, you need to add >/dev/null 2>&1 to the end of the executed command, for example:
0 3 * * * /root/backup.sh >/dev/ null 2>&1
If you want to send the output content to a specific email account, such as vivek@nixcraft.in, then you need to define a MAILTO variable as follows:
MAILTO="vivek@nixcraft.in. in"0 3 * * * /root/backup.sh >/dev/null 2>&1
Visit "Disable Email Tips for Crontab Commands" for more information.
Tasks: List all your scheduled tasks
Type the following command:
# crontab -l# crontab -u username -l
To delete all scheduled tasks, you can use the following command:
### Delete the current scheduled task crontab -r
### Delete the scheduled task under a certain user name. This command needs to be executed as root user crontab -r -u username
Use special strings to save time
You can use the following One of 8 special strings replaces the first five fields, saving you time and improving readability.
Example:
Run ntpdate command every hour
@hourly /path/to/ntpdate
More about /etc/crontab files and /etc/cron.d/* directories
/etc/crontab is the system's crontab file. Typically only used by the root user or daemons to configure system-level tasks. Each individual user must use the crontab command as described above to install and edit their own tasks. The /var/spool/cron/ or /var/cron/tabs/ directory stores the crontab file of individual users, which should be backed up in the user's home directory.
Understand the default /etc/crontab file
The typical /etc/crontab file content is like this:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/# run-parts01 * * * * 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
First, the environment variables must be defined. If the SHELL line is omitted, cron uses the default sh shell. If the PATH variable is omitted, there is no default search path and all files need to be located using absolute paths. If the HOME variable is omitted, cron will use the caller's (user's) home directory instead.
In addition, cron will read files in the /etc/cron.d/ directory. Normally, system daemons like sa-update or sysstat store their scheduled tasks here. As the root user or super user, you can use the following directories to configure your scheduled tasks. You can just put your script here. The run-parts command will run scripts or programs located in a certain directory through the /etc/crontab file.
Backup scheduled task
# crontab -l > /path/to/file# crontab -u user -l > /path/to/file