Compile a crontab scheduler task in the CentOS Environment
During host management, we often do some things every day. To avoid manual operations every time, we can write scheduled tasks to automatically execute the host periodically according to the schedule.
Install and start
Under CentOS:
Yum install vixie-cron
Yum install crontabs
The former is usually installed on the latter.
Start the service.
Service crond start
You must start the service.
System-level crontab
Use ls/etc/cron and double-click the TAB to view the relevant files and directories.
Cron. d/cron. daily/cron. hourly/cron. monthly/crontab cron. weekly/
File and directory Interpretation
You can edit the crontab file to create a scheduled task.
The daily, monthly, weekly, and monthly tasks are stored in directories suffixed with daily, hourly, weekly, and monthly.
The stored Shell script file has the permission of 755. We can write the task to be executed as a Shell script and run the corresponding directory.
The scheduled tasks with irregular cycles are placed under the corn. d directory. It can be seen as a supplement to the crontab file.
Crontab file format
This file contains necessary environment variables at the beginning.
For details, see the source file comment:
. ---------------- Minute (0-59)
|. ------------- Hour (0-23)
|. ---------- Number (1-31)
|. ------- Month (1-12) or jan, feb, mar, apr...
|. ---- Week (0-6) (Sunday = 0 or 7) or sun, mon, tue, wed, thu, fri, sat
|
* ***** Command to be executed by the user name
There are a total of seven fields, and their meanings are clear at a glance. Note the writing of each field.
** Indicates each object. For example, * in the minute field indicates execution every minute.
**/N indicates every n. For example, in the hour field, */2 indicates execution every two hours. 0-10/2 indicates, 9
* 1-5 indicates a continuous time period.
* 1, 3, 5 is used to enumerate several discontinuous periods.
Note:
1. The relationship between the third field 'day' and the fifth field 'Day of the Week' is or. So there is no conflict, and both cases are met.
2. If % exists in the file, escape it. For example, date + % w must be written as date + \ % w when writing the file.
3. Set the minute to * Be careful!
Crontab command (user level)
Command Options
Crontab [-u user] file
Crontab [-u user] [-e |-l |-r]
Option description
E. edit a scheduled task
L list scheduled tasks
R delete a scheduled task
I confirm before deletion
U indicates the user
S selinux context
The s option is not commonly used.
In fact, this command edits the/var/spool/cron/user name file.
You can edit the file directly.
The crond daemon reads the file once every minute.
Format
The format is similar to that of the crontab file in the previous article, but there is no user-name field.
This command can only compile the scheduled tasks of the current user.
Logs
You can view all the logs under/var/log/cron.
And/var/spool/cron/can also see some logs below.
Use crontab in Linux to create scheduled tasks
Routine scheduling of crontab in Linux
Linux crontab does not run troubleshooting
Ubuntu uses crontab for scheduled tasks
Linux scheduled task (at batch crontab anacron)