crontab 定时任务

通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本。时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合。这个命令非常适合周期性的日志分析或数据备份等工作。 一、命令格式 crontab [-u user] file crontab [-u user] [ -e | -l | -r ]

二、列出crontab任务文件 crontab -l

三、编辑crontab任务文件 crontab -e

四、ubuntu下启动、停止与重启cron

sudo /etc/init.d/cron start    //启动服务
sudo /etc/init.d/cron  stop     //关闭服务
sudo /etc/init.d/cron  restart  //重启服务
sudo /etc/init.d/cron reload   //重新载入配置
sudo service crond status    //查看服务状态

五、crontab实例

# 每天6点 
0 6 * * * /root/lnmp1.5/tools/backup.sh // 定时备份 
每隔2天6点
0 6 */2 * * /root/lnmp1.5/tools/backup.sh // 定时备份
每月1号6点
0 6 1 * *  /root/lnmp1.5/tools/backup.sh // 定时备份
每小时
* */1 * * *  /root/lnmp1.5/tools/backup.sh 
每半小时
*/30 * * * * /etc/init.d/zerotier