注意:centos在执行定时任务时若为sh脚本,则在定时任务中command前不需要sh,但debain需要。
一个坑:一个定时任务没有执行,搞了一天,最后发现是脚本的问题。
注意:在执行脚本是需要确定脚本具有可执行权限
要点:
- “*/”表示“每”,eg:*/10 * * * * [command]表示:每十分钟执行一次command
- “,”表示时间段,如:8,10,表示8和10
- “-”表示连续时间,如:7-9,表示7到9
一、centos系统
下载安装:yum install cronie
普通用户:
- crontab -e 添加定时任务,一般会出现一个空的文件,格式一样。
系统用户:
- crontab -e,添加定时任务
- 编辑 /etc/crontab
启、停、重启
service crond start
service crond stop
service crond restart
二、debain系统
官方文档(English):https://debian-handbook.info/browse/stable/sect.task-scheduling-cron-atd.html
下载安装:apt-get install cron
一般用户
- crontab -e ,添加定时任务
系统用户
- crontab -e
- 编辑 /etc/crontab
启、停、重启
/etc/init.d/cron start
/etc/init.d/cron stop
/etc/init.d/cron restart
更改crontab的默认编辑器:select-editor(*在centos不成功)
更多参考
http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html
https://www.cnblogs.com/YangJieCheng/p/7991660.html