crontab的简单使用
使用crontab可以在指定的时间执行一个shell脚本或者一系列Linux命令。例如系统管理员安排一个备份任务使其每天都运行
安装命令:yum install crontabs
服务操作
启动服务:service crond start
systemct start crond
关闭服务:service crond stop
systemct stop crond
重启服务: service crond restart
systemct restart crond
重新载入配置:service crond reload
systemct reload crond
查看服务状态:service crond status
systemct status crond
crontab 参数
crontab -e:编辑定时任务
crontab -r:/var/spool/cron目录中删除用户的crontab文件
crontab -l > my_crondback.txt :备份crontab文件
crontab 日志路径
/var/log/cron* :只记录是否执行了某些计划的脚本日志
如果要查看执行的脚本是否有问题,可以使用日志可以在脚本添加
比如 : * * * * * /root/test.sh > /root/test.log 2>&1
或者直接在shell脚本:
crond_test.sh
#!/bin/bash
source /etc/profile
/usr/local/bin/python3 /root/crond_test.py >> /root/crond_test.log 2>&1
1
2
3
crontab -e 的内容:
*/2 * * * * /root/crond_test.sh
crontab 命令示例
实例1:每1分钟执行一次myCommand
* * * * * Command
1
实例2:每小时的第3和第15分钟执行
3,15 * * * * Command
1
实例3:在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * Command
1
实例4:每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * Command
1
实例5:每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 Command
1
实例6:每晚的21:30执行
30 21 * * * Command
沈阳众诚志联真诚为您服务