Scheduled task, the task is planned to perform at the expected time of the meeting. Use crond service in linux system to do the job, we can cron is set to start automatically at boot time. After cron starts, it reads all of its configuration file (global configuration file / etc / crontab, and each user's scheduled tasks configuration file), then the time will come to call the cron tasks according to the degree of command and execution time.
0, crond program tasks related commands to configure
name | Types of | Explanation |
---|---|---|
crontab | command | crontab command, you can create, edit, delete a user's regular tasks |
/etc/crontab | file | Scheduled Tasks system global configuration file |
/ Var / spool / cron / | table of Contents | Store with your name of the user program task configuration file, by editing the crontab command |
/etc/cron.deny | file | Set which users can not use crontab function |
/etc/cron.hourly/ | table of Contents | Store script executed once every hour |
/etc/cron.daily/ | table of Contents | Store script executed once a day |
/etc/cron.monthly/ | table of Contents | The script is stored once a month |
/etc/cron.weekly/ | table of Contents | Store script executed once a week |
/etc/cron.d/ | table of Contents | File system tasks to be performed automatically on a regular basis |
Note:
1, add a user's scheduled tasks via crontab command will generate a file named names to the user in the / var / spool / cron directory, the crontab command to edit the file it is.
2, crond service not only to read every minute of all the files in a / var / spool / cron, also need time to read / etc / crontab, so we can use this file to configure crond service to do something. Configured with the crontab command is for a user, and edit / etc / crontab is the task for the system.
1, to see whether the system is installed crondtab
rpm -qa|grep crontab
2, to see whether to start the service crond
ps -ef|grep crond
orservice crond status
3, how to start and stop the service crond
- Start
/etc/init.d/crond start
orservice crond start
- Stop
/etc/init.d/crond stop
orservice crond stop
- Reboot
/etc/init.d/crond restart
- Reload the configuration file (typically performed after setting the scheduled task, it can take effect immediately, without waiting for the system to read configuration files)
/etc/init.d/crond reload
4, set the boot from the start crond service
chkconfig crond on
5, crontab command instructions
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) 删除用户的计划任务(指删除该用户的所有计划任务,强烈建议使用-ir)
-i (prompt before deleting user's crontab) 删除用户的计划任务前进行提示
-s (selinux context)
Example 1: Edit the current scheduled task logged-on user
crontab -e
Example 2: lists the test user's scheduled tasks
crontab -l test -e
6 format, scheduled tasks file
* * * * * command
分 时 天 月 周 要执行的命令
symbol | Explanation |
---|---|
Minute | Denotes minutes, it can be any integer between 0 to 59 |
Time | Means hour, in the range [0 to 23] |
day | In the range [1 to 31] |
month | In the range [1 to 12] |
week | In the range [0 to 7], 0 or 7 for Sunday |
command | Command to be executed, the system can command, you can also write your own script |
* | Each represents a meaning, such as location points for the * represents be performed every minute, represents all values from 0 to 59 |
, | Separator, when taking a plurality of values corresponding to a domain using, for example, minute domains "3" represents a sub-hour and 3 minutes scheduled task execution |
- | It represents the time range, for example, 2-6 means "2,3,4,5,6" |
/ | Can be used to specify the time interval, e.g., "* / 5" used sequentially performed every 5 minutes at minute domain |
Example 1: every day 0:00 time-sharing, execute test script
0 0 * * * test.sh
Example 2: test script execution at every 3 hours
* */3 * * * test.sh
Example 3: Monday to Friday 0:00 execute test scripts
0 0 * * 1-5 test.sh