Linux crontab command details, crontab command details
Crontab is a convenient command to execute a task in a scheduled (Cyclic) manner on unix/linux systems. The linux system is controlled by the cron (crond) System Service. The Linux system has a lot of planned work, so this system service is started by default. To understand the crontab command, you must first know the crond service, so if you do not cut the firewood by mistake, first learn about the crond service.
First knowledge of Crond
Crond is a daemon in linux that is used to periodically execute a task or wait for processing some events. It is similar to a scheduled task in windows. After the operating system is installed, by default, the service tool is installed and the crond process is automatically started. The crond Process regularly checks whether there are tasks to be executed every minute. If there are tasks to be executed, the task is automatically executed.
Familiar with Linux, there are two types of task scheduling in Linux: System Task Scheduling and user task scheduling.
System Task Scheduling: the tasks that the system periodically performs, such as writing cached data to the hard disk and clearing logs. There is a crontab file under the/etc directory, which is the configuration file for system task scheduling.
The/etc/crontab file contains the following lines:
[root@lychao ~]# cat /etc/crontab SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=""HOME=/# run-parts51 * * * * root run-parts /etc/cron.hourly24 7 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly[root@lychao ~]#
The first four rows are used to configure the environment variables for running crond tasks. The first line of SHELL variables specifies the shell to be used by the system. bash is used here, the PATH variable in the second line specifies the PATH for the system to execute the command. The MAILTO variable in the third line specifies that the crond task execution information will be sent to the root user by email. If the value of MAILTO variable is null, the task execution information is not sent to the user. The HOME variable in the fourth line specifies the main directory used for executing commands or scripts.
User task scheduling: jobs that you regularly perform, such as user data backup and scheduled email reminders. You can use the crontab tool to customize your own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. The file name is the same as the user name.
In Linux, we can use the following commands to view the crond service status:
[root@lychao ~]# /sbin/service crond status
Start, stop, and restart the crond service:
[root@lychao~]# /sbin/service crond start[root@lychao~]# /sbin/service crond stop[root@lychao~]# /sbin/service crond restart
Reload the service
[Root @ lychao ~] #/Sbin/service crond reload // reload the configuration
The system starts the crond service automatically. Add a line in the/etc/rc. d/rc. local file:
<pre name="code" class="plain">[root@lychao~]# /sbin/service crond start
Start the crontab service manually:
service crond start
Run the following command to check whether the crontab service is set to start at startup:
ntsys
Meanings of parameters of the crontab command:
Through the crontab command, we can execute the specified system command or shell script at a fixed interval. The time interval can be any combination of minutes, hours, days, months, weeks, and above. This command includes non-permanent and periodic log analysis or data backup.
In the crontab file created by the user, each row represents a task, and each field in each row represents a setting. Its format is divided into six fields. The first five fields are time sets, the sixth part is the command segment to be executed. The format is as follows:
Minute hour day month week command
Where:
Minute: minute (0--59), which can be any integer from 0 to 59.
Hour: hour (0---23), which can be any integer between 0 and 23.
Day: represents the date (1----31), which can be any integer from 1 to 31.
Month: represents the month (1---12), which can be any integer from 1 to 12.
Week: the day of the week (0---6). It can be any integer from 0 to 6. Here 0 represents Sunday.
Command: the command to be executed. It can be a system command or a script file compiled by yourself.
In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:
Every morning
0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.
Every two hours
0 */2 *** echo "Have a break now.">/tmp/test.txt
Every two hours from PM to am, am
0 23-7/2, 8 *** echo "Have a good dream :)">/tmp/test.txt
Am from Monday 4 to Wednesday every week
0 11 4*1-3 command line
Am, January 1, January 1
0 4 1 1 * command line
The following figure shows the parameters of the crontab command.
Listing crontab Files
To list crontab files, you can use:
$ Crontab-l
Edit the crontab file
If you want TO add, delete, or edit entries in the crontab file, and set the environment variable e d I to r to v I, you can use v I TO edit the crontab file, the corresponding command is:
$ Crontab-e
Delete A crontab file
To delete the crontab file, you can use:
$ Crontab-r
When the crontab suddenly fails, try/etc/init. d/crond restart to solve the problem. Or check the log to see whether a job has been executed or whether tail-f/var/log/cron has been reported. Do not run crontab-r in disorder. It deletes your Crontab file from the Crontab directory (/var/spool/cron. All crontabs deleted from the user are gone.
In crontab, % indicates a line break. If you want to use it, you must escape \ %. For example, the commonly used date '+ % Y % m % d' will not be executed in crontab, change to date '+ \ % Y \ % m \ % d '.