二、周期性执行计划任务crontab
crontab -e + user:为某用户编辑计划任务
crontab的编辑格式:
field allowed values
----- --------------
minute分 0-59
hour时 0-23
day of month天 1-31
month月 1-12 (or names, see below)
day of week星期 0-7 (0 or 7 is Sunday, or use names)
*:代表每的概念,当使用整点时间的时候,如:7:00,则分需要写0
/:代表每隔的意思
0,10,20,30 :代表10,20,30分别执行
1-5:代表1到5
分时天月是和的关系
分时天月周是和的关系:
分 时 天 月 周 命令
0 7 * * * /bin/bash
0,10,20,30 7 * * * /bin/bash
0,10,20,30 7 * * 1-5 /bin/bash
0,10,20,30 7 * * 1-3,6 /bin/bash
0-15/5 7 * * 1-5 /bin/bash
0 7 * 2-12/2 * /bin/bash
0 7 * 1-11/2 * /bin/bash
0 7 1-10 1-6 * /bin/bash
0 7 * 1-6 1-5 /bin/bash
0 7 1-10 1-6 1-5 /bin/bash
因为天和周有可能有冲突:所以天和周同时写的时候,它们是或者的关系
crontab的计划任务内容保存在目录/var/spool/cron下面
crontab -r :删除计划任务
控制用户是否能做计划任务的文件为:
/etc/cron.allow
/etc/cron.deny
/etc/cron.daily /etc/cron.monthly 目录分别放着每天和每月执行的脚本,可以讲脚本放到这些目录下面,然后定期执行;
/tmp目录定期清理工具:tmpwatch
让计划任务按照每隔多少秒执行,使用sleep命令:
* * * * * echo 'Hello' &>>/tmp/cron
* * * * * sleep 1;echo 'world' &>>/tmp/cron