資料來源:https://www.shiyanlou.com/courses/1/labs/1918/document
1 crontab 環(huán)境準(zhǔn)備
sudo apt-get install -y rsyslog 安裝rsyslog
sudo service rsyslog start 啟動 rsyslog(在自己本地 Ubuntu中 會默認(rèn)自行啟動)
sudo cron -f & 啟動crontab(本地 Ubuntu 的環(huán)境中不需要手動啟動)
crontab -e 添加一個計劃任務(wù)
2 任務(wù)添加和查看
在最后一行添加每分鐘創(chuàng)建一個文件的任務(wù)
crontab -l 查看添加了哪些任務(wù)
ps aux | grep cron 查看任務(wù)有沒有執(zhí)行
sudo tail -f /var/log/syslog 從日志中看任務(wù)詳情
crontab -r 刪除任務(wù)
ll /etc/crontab 執(zhí)行的相關(guān)記錄
每個目錄的作用:
/etc/cron.daily,目錄下的腳本會每天執(zhí)行一次感凤,在每天的6點25分時運行膀藐;
/etc/cron.hourly锈玉,目錄下的腳本會每個小時執(zhí)行一次,在每小時的17分鐘時運行雪隧;
/etc/cron.monthly属瓣,目錄下的腳本會每月執(zhí)行一次,在每月1號的6點52分時運行棒仍;
/etc/cron.weekly悲靴,目錄下的腳本會每周執(zhí)行一次,在每周第七天的6點47分時運行降狠;
系統(tǒng)默認(rèn)執(zhí)行時間可以根據(jù)需求進行修改对竣。
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的整數(shù)
hour - 從0到23的整數(shù)
day - 從1到31的整數(shù) (必須是指定月份的有效日期)
month - 從1到12的整數(shù) (或如Jan或Feb簡寫的月份)
dayofweek - 從0到7的整數(shù)庇楞,0或7用來描述周日 (或用Sun或Mon簡寫來表示)
command - 需要執(zhí)行的命令(可用as ls /proc >> /tmp/proc或 執(zhí)行自定義腳本的命令)