资料来源:https://www.shiyanlou.com/courses/1/labs/1918/document
1 crontab 环境准备
sudo apt-get install -y rsyslog 安装rsyslog
sudo service rsyslog start 启动 rsyslog(在自己本地 Ubuntu中 会默认自行启动)
sudo cron -f & 启动crontab(本地 Ubuntu 的环境中不需要手动启动)
crontab -e 添加一个计划任务
2 任务添加和查看
在最后一行添加每分钟创建一个文件的任务
crontab -l 查看添加了哪些任务
ps aux | grep cron 查看任务有没有执行
sudo tail -f /var/log/syslog 从日志中看任务详情
crontab -r 删除任务
ll /etc/crontab 执行的相关记录
每个目录的作用:
/etc/cron.daily,目录下的脚本会每天执行一次,在每天的6点25分时运行;
/etc/cron.hourly,目录下的脚本会每个小时执行一次,在每小时的17分钟时运行;
/etc/cron.monthly,目录下的脚本会每月执行一次,在每月1号的6点52分时运行;
/etc/cron.weekly,目录下的脚本会每周执行一次,在每周第七天的6点47分时运行;
系统默认执行时间可以根据需求进行修改。
3 时间格式说明
参考资料:http://blog.chinaunix.net/uid-7552018-id-182133.html
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
minute hour day month dayofweek command
minute - 从0到59的整数
hour - 从0到23的整数
day - 从1到31的整数 (必须是指定月份的有效日期)
month - 从1到12的整数 (或如Jan或Feb简写的月份)
dayofweek - 从0到7的整数,0或7用来描述周日 (或用Sun或Mon简写来表示)
command - 需要执行的命令(可用as ls /proc >> /tmp/proc或 执行自定义脚本的命令)