schedule with cron
>>>schedule the execution of scripts at a given time or time intervals.
>>>cron is such a utility that allows tasks to automatically run in the background of the system at regular intervals by using the cron daemon.
>>>the command crontab is used to add schedule entity to the cron schedule domain.
>>>a cron schedule is a simple text file.
1:crontab约束:cron.allow & cron.deny
当用户名记录在文件/usr/lib/cron/cron.allow时,该用户的crontab有效。
当/usr/lib/cron/cron.allow文件不存在时,用户名没被记录在/usr/lib/cron/cron.deny文件中,该用户的crontab也有效。
当只存在/usr/lib/cron/cron.deny且为空时,所有的用户都可使用crontab。
当前两个文件都不存在时,只有root用户可以使用crontab。
Deny或allow记录以每行一个用户名的形式记录信息。
2:crontab选项
crontab -l ##列出所有的crontab
crontab -e ##已vim的形式,编辑crontab
crontab -r ##删除所有的cronjob
crontab -u ##指定crontab的用户
3:crontab命令
* * * * * command
第一个*指分钟,即每分钟都执行,取值范围0:59
第二个*指小时,即每小时都执行,取值范围0:23
第三个*指天, 即每天都执行,取值范围1:31
第四个*指月, 即每月都执行,取值范围1:12
第五个*指周, 即一周内每一天都执行,取值范围0:6
command指待执行的命令,涉及到的文件命令最好用绝对路径
* 代指每一个
, 代指连接某几个
/ 代指间隔多长时间执行一次
4:实例
30 6 * * * sh /user/example.sh ##每天早上六点半执行一次example.sh脚本
30 6,7,8 * 2 * sh /user/example.sh ##2月份中每天早上的6:30,7:30和8:30分别执行一次example.sh脚本
*/10 * * * 0 sh /user/example.sh ##一到周日每隔10分钟执行一次example.sh脚本
/*############################################################*/
补充:20191030
如果是系统第一次使用CRONTAB,系统会提示
no crontab for *** - using an empty one
这里需要为即将创建的crontab选择一下编辑器。
![0afca4e4a939d74a047d6ea131c42fbc.png](https://img-blog.csdnimg.cn/img_convert/0afca4e4a939d74a047d6ea131c42fbc.png)
图片中的“2”表示选择VIM编辑器,然后即可进入CRONTAB编辑界面,创建成功。
![c3ea483ed29021705834cc664bdb4229.png](https://img-blog.csdnimg.cn/img_convert/c3ea483ed29021705834cc664bdb4229.png)