When managing a host, we have some things to do every day, in order to avoid the manual operation every time, we can write a scheduled task to allow the host to periodically follow the plan to automatically execute. Install startup
CentOS under:
Yum install Vixie-cron
yum install Crontabs
Installation of the former will generally be the same installation of the latter.
Start the service.
Service Crond Start
To start the service. system-Level crontab
Use Ls/etc/cron and then hit the tab two, you can see the relevant files and directories.
cron.d/ cron.daily/ cron.hourly/ cron.monthly/crontab cron.weekly/
interpretation of documents and catalogues
We can edit the crontab file to create a scheduled task.
And the daily,hourly,weekly,monthly suffix of the directory to store the daily, monthly, weekly, monthly tasks performed.
Where the shell script file is stored, permission 755. We have to do the task as a shell script to throw the corresponding directory.
The irregular cycle of scheduled tasks is placed under the CORN.D directory. Can be seen as a supplement to the crontab file. crontab file Format
The file contains the necessary environment variables at the beginning and is no longer described.
Read the source file annotation directly:
.----------------minutes (0-59)
|-------------hours (0-23)
----------Date (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
| | | | |
* * * * * User name the command to be executed
A total of seven fields, its meaning at a glance. What you need to be aware of is the spelling of each field.
* * means every one. For example, the minute field under * means every minute of execution
* */n means every n. For example, under the hour field, */2 says it executes every two hours. 0-10/2 says 1,3,5,7,9
* 1-5 This format represents a continuous period of time
* 1,3,5 This format is to enumerate several discontinuous time periods
Attention:
1. The third field ' days ' and the Fifth field ' Day of the week ' are or relationship. So there will be no conflict, both of which are executed.
2. If% of this file is to be escaped, such as date +%w written in the file to be written as date +\%w
3. Set the minutes into * be careful. crontab Command (User level) Command Options
crontab [-u user] file
crontab [-u user] [-e|-l|-r]
Options |
Description |
E |
Edit Scheduled Task |
L |
List Scheduled Tasks |
R |
Delete a scheduled task |
I |
Confirm before deleting |
U |
Indicate the owning user |
S |
SELinux context |
The S option is not used.
In fact, the command edits the /var/spool/cron/username file.
So you can also edit the file directly.
The daemon Crond reads the file once per minute. Writing Format
The format is similar to the format of the crontab file in the previous article, but there is no user user-name field.
because the command can only write scheduled tasks for the current user . Log
You can see all the logs under/var/log/cron.
And/var/spool/cron/can also see a certain log below.