crontab(选项)(参数)python
[root@xuexi-001 ~]# service crond status ······//检查crond 服务状态 Redirecting to /bin/systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 日 2018-06-17 14:18:25 CST; 7h ago Main PID: 541 (crond) CGroup: /system.slice/crond.service └─541 /usr/sbin/crond -n 6月 17 14:18:25 xuexi-001 systemd[1]: Started Command Scheduler. 6月 17 14:18:25 xuexi-001 systemd[1]: Starting Command Scheduler... 6月 17 14:18:26 xuexi-001 crond[541]: (CRON) INFO (RANDOM_DELAY will be ...) 6月 17 14:18:26 xuexi-001 crond[541]: (CRON) INFO (running with inotify ...) Hint: Some lines were ellipsized, use -l to show in full.
列出服务项须要执行的操做shell
[root@xuexi-001 ~]# service crond The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[root@xuexi-001 ~]# crontab -e ··· 编辑crontab任务 */1 * * * * date >> /tmp/date.txt [root@xuexi-001 ~]# crontab -l ····· 列出当前crontab 任务 */1 * * * * date >> /tmp/date.txt [root@xuexi-001 ~]# date 2018年 06月 17日 星期日 21:43:17 CST [root@xuexi-001 ~]# tail -f /tmp/date.txt ······ 动态显示date.txt文件内容 2018年 06月 17日 星期日 21:43:01 CST 2018年 06月 17日 星期日 21:44:01 CST 2018年 06月 17日 星期日 21:45:01 CST
系统服务 crond:每分钟都会从配置文件刷新定时任务。apache
配置文件:文件方式设置定时任务。工具
配置工具crontab:用于调整定时任务。rest
# .---------------- 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
案例日志
每晚的21;30重启apachecode
每个月一、十、22日的4:45重启apachecrontab
每个月1到10日的4:45重启apacheip
每隔两分钟重启apacheit
晚上11点到早上7点之间,每隔一小时重启apache
天天18:00至23:00之间每隔30分钟重启apache
第三和第五个域之间执行的是“或”操做
59 1 1-7 4 *test ` date+%w ` -eq 6 && /root/a.sh
%w 一星期中的第几日(0-6),0 表明周一
两小时运行一次
0 */2 * * * date
案例: crontab 中最小只能设置到每分钟执行一个命令,若是想每半分钟执行某个命令怎么作到呢?
[root@xuexi-001 ~]# crontab -e */1 * * * * date >> date.log */1 * * * * sleep 30s; date >> date.log [root@xuexi-001 ~]# cat date.log 2018年 06月 18日 星期一 00:34:01 CST 2018年 06月 18日 星期一 00:34:32 CST 2018年 06月 18日 星期一 00:35:01 CST 2018年 06月 18日 星期一 00:35:31 CST