Crontab 備忘

cron 是處理定時任務的程序,來源是希臘語裏面意思是「時間」的某個詞,能夠查看它是否是在運行 ps -ef | grep cron | grep -v grep,crontab 裏面內容能執行的前提是 cron 必須在運行。
crontab 是 Cron Table 的意思。每一個用戶都有本身的,能夠在 /var/spool/cron 找到這些文件。可是不要直接編輯它。而是經過命令,分別是列出內容、編輯、刪除:php

$ crontab -l
$ crontab -e
$ crontab -r

想備份的話直接把 crontab -l 的內容保存到文件就行了。html

另外系統級別還有這樣的目錄 /etc/cron.{hourly,daily,weekly,monthly},裏面的內容會按照對應的週期執行。linux

Crontab 裏面每一項的設置的規則以下:dom

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed

能夠寫一個 Hello World 來測試,會每分鐘把時間打在 ~/date_log 裏面:測試

* * * * * date >> date_log

執行 Crontab 裏面的命令時產生的 stdout 會經過郵件發送給當前用戶。有郵件以後命令行中會提示 You have new mailWhat is the "You have new mail" message in Linux/UNIX? - Super Userui

再多設置幾個來作測試吧!url

# m h  dom mon dow   command
* * * * * date >> ~/cron_test/every_minute
10 * * * * date >> ~/cron_test/every_hour_10
44 4 * * * date >> ~/cron_test/every_day_4_44
0 4 1 * * date >> ~/cron_test/every_month_1_4_00

參考:操作系統

相關文章
相關標籤/搜索