[[email protected] ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab) 編輯crontab 工作内容
-l (list user's crontab) 檢視crontab工作内容
-r (delete user's crontab) 删除所有的crontab任務
-i (prompt before deleting user's crontab)
-s (selinux context)
預設情況下,任何使用者隻要不被列入/etc/cron.deny 當中,都可以執行”crontab �Ce” 去編輯自己的例行性指令了,
代表的意義
分鐘
小時
日期
月份
周
數字範圍
0-59
0-23
1-31
1-12
0-7
周的數字為0-7時,都代表”星期天”的意思,
輔助字元
特殊字元
代表意義
*(星号)
代表任何時刻都接受的意思,
,(逗号)
代表分割時段的意思,如果要執行2:00與4:00時,
0 2,4 * * * command
時間參數有五列,第二列就是2,4 代表2:00 與4:00
-(減号)
代表一段時間的範圍,例如7點到10點之間的每小時10分鐘都進行一次
工作: 10 7-10 * * * command
第二列變成7-10 代表7 8 9 10
/n(斜線)
那個n代表數字,即是每隔n機關間隔的意思,例如每5分鐘進行一次,
則: */5 * * * * command
也可以寫成0-59/5 意思相同
系統任務計劃
[[email protected] ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# 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) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
crontab -e是針對使用者的cron來設計的。
基本上cron這個服務的最低檢測限制是”分鐘”,是以cron每分鐘去讀取一次/etc/crontab 與/var/spool/cron 裡面的資料内容。
标準輸出(stout): 代碼為1 使用>或>>
标準錯誤輸出(stderr): 代碼為2 使用2> 或2>>
黑洞/dev/null
錯誤輸出 2>&1
cron 讓計劃任務不在螢幕上輸出采用 > /dev/null 2>&1
[[email protected] ~]# crontab -l
*/1 * * * * echo "hello" >> /tmp/test.txt
[[email protected] ~]# cat /tmp/test.txt
hello
[[email protected] ~]# cat /tmp/test.txt
hello
hello
讓輸出到黑洞裡面去
[[email protected] ~]# crontab -l
*/1 * * * * echo "hello"
[[email protected] ~]# crontab -l
*/1 * * * * echo "hello" >/dev/null 2>&1 輸出到黑洞了
其他的幫助可以檢視 man cron 或者man crontab