Cron is a daemon that can be used to schedule the execution of repeated tasks based on the combination of time, date, month, and week.
Cron assumes that the system continues to run. If the system does not run when a task is scheduled, the task will not be executed.
To use the cron service, you must installVixie-cronRpm software package, and must be runningCrondService. To determine whether the software package is installed, useRpm-Q vixie-cronCommand. To determine whether the service is running, use/Sbin/service crond statusCommand.
37.1.1. configure a Cron task
The main configuration file of cron is/Etc/crontab, Which includes the following lines:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * 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 |
The first four rows are variables used to configure the running environment of the cron task.ShellThe value of the variable tells the system which Shell environment to use (BASH Shell in this example );PathVariable defines the path used to execute commands. Cron task output is mailedMailtoThe username defined by the variable. IfMailtoThe variable is defined as a blank string (Mailto = ""), The email will not be sent.HomeVariables can be used to set the main directory used for executing commands or scripts.
/Etc/crontabEach row in the file represents a task in the format:
minute hour day month dayofweek command |
Minute-Minute, any integer between 0 and 59
Hour-Hour, any integer between 0 and 23
Day-Date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)
Month-The month, any integer from 1 to 12 (or the month can be abbreviated as Jan or FeB)
Dayofweek-Any integer between 0 and 7 in a week. Here, 0 or 7 represents Sunday (or the abbreviation of a week, such as sun or Mon)
Command-Command to be executed (the command can beLs/proc>/tmp/procSuch commands can also be the command to execute your own script .)
In any of the preceding values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that this command is executed every month after other constraints are met.
The short line (-) between integers specifies an integer range. For example,1-4It indicates integers 1, 2, 3, and 4.
Specifies a list of values separated by commas. For example,3, 4, 6, 8Indicate the four specified integers.
A forward slash (/) can be used to specify the interval. Add/<Integer>This means that integer can be skipped within the range. For example,0-59/2It can be used to define every two minutes in the minute field. The interval value can also be used with the star number. For example,*/3The value can be used in the month field to indicate that a task is run every three months.
The line starting with # Is a comment and will not be processed.
If you are/Etc/crontabAs seen in the file, it usesRun-PartsScript Execution/Etc/cron. Hourly,/Etc/cron. daily,/Etc/cron. WeeklyAnd/Etc/cron. MonthlyScripts in the directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.
A cron task can be added/Etc/cron. dDirectory. Use and/Etc/crontab.
# record the memory usage of the system every monday # at 3:30AM in the file /tmp/meminfo 30 3 * * mon cat /proc/meminfo >> /tmp/meminfo # run custom script the first day of every month at 4:10AM 10 4 1 * * /root/scripts/backup.sh |
Example 37-1. crontab
Users other than root users can useCrontabTool to configure cron tasks. All user-defined crontabs are saved in/Var/spool/cronDirectory, and use the user identity to create them for execution. Create a crontab project as a user, log on as the user, and then typeCrontab-eCommandVisualOrEditorThe editor specified by the environment variable to edit the crontab of the user. The format and/Etc/crontabSame. After the changes made to crontab are saved, the crontab file is saved according to the user name and written to the file./Var/spool/cron/Username.
Cron Daemon checks every minute/Etc/crontabFile,Etc/cron. d/Directory, and/Var/spool/cronDirectory changes. If changes are found, they are loaded into the memory. In this way, you do not have to restart the daemon after a crontab file is changed.
37.1.2. Control the use of Cron
/Etc/cron. allowAnd/Etc/cron. denyFiles are used to restrict the use of cron. The two control files are in the format of one user per line. Spaces are not allowed for both files. If the control file is modified, the cron daemon (Crond) Does not need to be restarted. The control file is read every time you add or delete a Cron task.
Regardless of the rules in the control file, the root user can always use cron.
IfCron. allowThe file exists. Only the users listed in the file are allowed to use Cron, andCron. denyFiles are ignored.
IfCron. allowThe file does not exist.Cron. denyAll users listed in are forbidden to use cron.
37.1.3. Start and Stop services
To start the cron service, use/Sbin/service crond startCommand. To stop the service, use/Sbin/service crond stopCommand. We recommend that you start this service during boot.
Crontab usage
Crontab task format
* *** Task
Task by hour, day, month, and week
1st columns minute 1 ~ 5 9 (0 indicates 0 points)
2nd columns hour 1 ~ 2 3 (0 indicates midnight)
3rd columns daily 1 ~ 3 1
4th columns Month 1 ~ 1 2
5th columns in a week 0 ~ 6 (0 indicates Sunday)
6th columns of commands to run
Some examples:
30 21 ***/apps/bin/cleanup. Sh
The example above indicates that C l e a n u p. s h in the/a p s/B I n directory of 2 1: 3 0 every night.
45 4, 10, 22 **/apps/bin/backup. Sh
The example above indicates the 4: 4 5 running/a p s/B I n directory B A C K u p on the 1, 1, 0, and 2 every month. s h.
10 1 ** 6, 0/bin/find-name "core"-exec RM {}/;
The preceding example indicates that an f I n d command is run at every Saturday and Sunday.
0, 30 18-23 ***/apps/bin/dbcheck. Sh
The preceding example indicates that every day 1 8: 0 0 to 2 3: run between 0 and every 3 0 minutes/a p s/B I n Directory d B C H E C K. s h.
0 23 ** 6/apps/bin/qtrend. Sh
The above example indicates that every Saturday 11: 0 0 p m runs/a p s/B I n directory Q T r e n d. s h
Two ways to implement scheduled tasks
One is to edit a file and load it with crontab
One is to use crontab-E for direct editing.
My own example
Break into a pile of characters into a file every minute
First VI crontab1
# Every minute echo Char to file
1-59 **** echo 'date' "LJ/N">/opt/Lijie/lj.txt
It is best to add comments
In this way, the purpose of the current scheduled task can be clearly defined.
Then crontab crontab1 loads the scheduled task crontab1.
The other is
Crontab-e
# Every minute echo Char to file
1-59 **** echo 'date' "LJ/N">/opt/Lijie/lj.txt
Date is enclosed in single quotes, indicating that if date is a command, it will be executed as a command.
If you use date directly, the output is the string "date", rather than the execution result of the date command.
>>Is appended to the end of the file, and> is directly overwriting the file
If crontab-e cannot be edited
So your environment variables are not configured.
Enter VI/etc/profile
Add
Editor = vi
Export Editor
Crontab-l list all scheduled tasks
To delete a scheduled task
So
Crontab-e
Comment out the corresponding scheduled task statement
For example, to delete the scheduled task that I just defined
# Every minute echo Char to file
1-59 **** echo 'date' "LJ/N">/opt/Lijie/lj.txt
Only
# Every minute echo Char to file
#1-59 **** echo 'date' "LJ/N">/opt/Lijie/lj.txt
If crontab-R is used
Delete all scheduled tasks.
Note:
1. If you use the crontab file to load a scheduled task, the scheduled task will be washed out.
Therefore, if you want to add a scheduled task, you 'd better use crontab-e to add a scheduled task.
Do not use the crontab file.
2. The crontab log file is/var/log/cron.
3. If you use crontab <FILENAME> to set Cron, pay attention to file garbled characters. I encountered a problem. The/var/log/cron log shows Dec 28 11:34:00 target crond [13449]: (hnuc) cmd (/home/hnuc/Shell/t. sh ^ m ~ M is garbled because: I used the cron file edited by editplus, and the carriage return line break symbol in it is dos rather than Linux or UNIX. Here ^ m is actually a DOS line break, which is parsed in Linux.