First, Introduction
1, Linux has many recurring tasks, these tasks are completed by the cron daemon, so cron This system service is started by default.
2, Linux scheduling tasks are divided into two categories: System task scheduling and user task scheduling, System task scheduling is the system periodic work to be performed. The/etc/crontab file is a configuration file for system task scheduling.
3, user task scheduling is the user to perform periodic work, you can use the crontab command to customize their own scheduled tasks, user-defined scheduled tasks are saved in the/var/spool/cron directory, the file name and user name consistent
4, user task scheduling users ' rights:
1),/etc/cron.deny, the user listed in the file is not allowed to use the crontab command
2),/etc/cron.allow, the user listed in the file allows the use of the crontab command
3),/var/spool/cron/, all users crontab files directory, file name with the user name
Second, crontab Timing Task command detailed
Cat/etc/crontab
Shell=/bin/bash #SHELL变量指定系统使用哪种shell, bash is used here
Path=/sbin:/bin:/usr/sbin:/usr/bin #PATH变量指定系统执行命令的路径
Mailto=root #MAILTO变量指定任务执行信息发送给谁
home=/#HOME变量指定执行命令或脚本时使用的主目录
# for details see Mans 4 Crontabs
# Example of Job definition:
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# | | .----------Day of Month (1-31)
# | | | .-------month (1-12) OR jan,feb,mar,apr ...
# | | | | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * * user-name command to be executed
1, the first 5 fields for the time set segment, the 6th field is required to execute the command segment
Minute hour day Month Week command
Minute hour date Month week number of commands or scripts to execute
The special characters that each field can use represent meaning:
* indicates that all possible values, such as Day is *, execute the command or script on a daily basis after the conditions of the other field are met;
, a comma-separated value can be used to specify a list range
-integer ranges can be represented by a middle bar between integers
/used to specify the interval of time, such as "0-23/2" means at 0 points ~23, every two hours, such as hour field "*/3" means to do every 3 hours
2. Crond Service
Install crond command: Yum install Crontabs
Service Operations Command:
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
/sbin/service crond Status//Start service
View Boot start service: NTSYSV
Join boot: Chkconfig-level crond on
3, crontab command detailed
The crontab command can execute a specified command or shell script at a specified time.
Crontab--help
Usage:crontab [-u user] File
crontab [-u user] [-e |-l |-r]
(default operation is replace, per 1003.2)
-E (Edit user ' s crontab)
-L (list user ' s crontab)
-R (Delete user ' s crontab)
-I (Prompt before deleting user ' s crontab)
-S (selinux context)
-U User: Sets the crontab service for a user, such as "-U root" to indicate the crontab service of the root user
-E: Edits a user's crontab file, does not use-u specifies that the current user's crontab file is edited, the same
-L: Displays the contents of a user's crontab file
-r: Remove a user's crontab file from the/var/spool/cron/directory
-I: Prompt user before deleting a user's Crontabz file
-s:selinux Content
4. For example:
Every Monday, three, five 3:00 the system enters the maintenance state and restarts the system.
0 * * 1,3,5 shutdown-r +5
1 minutes per hour execute the bin/account command in the user directory:
1 * * * * bin/account
Execute two instructions (each instruction is delimited) below the user directory every day 3:20 A.M.:
3 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)
Each year in January and April, 4th to 9th of 3:12 and 3:55 execute/bin/rm-f expire.1st This command, and add the results after mm.txt this file (mm.txt file is located in the user's own directory location).
12,55 3 5-9 1,4 */bin/rm-f expire.1st$#@62;$#@62;mm.txt
One Linux command per day--crontab