crontab定時任務
1.命令格式
usage: crontab [-u user] file crontab [-u user] [ -e | -l | -r ] (default operation is replace, per 1003.2) -e (edit user's crontab) -l (list user's crontab) -r (delete user's crontab) -i (prompt before deleting user's crontab) -s (selinux context)
2.命令參數
- -u user 用來設定某個用戶的crontab服務
- file 命令文件的名字。將file作爲crontab的任務列表文件並載入crontab。如果在命令行中沒有指定這個文件,crontab命令將接受標準輸入(鍵盤)上鍵入的命令,並將它們載入crontab。
- -e 編輯某個用戶的crontab文件內容。如果不指定用戶,則表示編輯當前用戶的crontab文件。
- -l 顯示某個用戶的crontab文件內容,如果不指定用戶,則表示顯示當前用戶的crontab文件內容。
- -r 從/var/spool/cron目錄中刪除某個用戶的crontab文件,如果不指定用戶,則默認刪除當前用戶的crontab文件。
3.crontab的文件格式
[root@study ~]# cat /etc/crontab SHELL=/bin/bash <==使用哪種 shell 介面 PATH=/sbin:/bin:/usr/sbin:/usr/bin <==執行檔搜尋路徑 MAILTO=root <==若有額外STDOUT,以 email將資料送給誰 # 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
0 12 * * * mail -s "at 12:00" dmtsai < /home/dmtsai/.bashrc
#分 時 日 月 週 |<==============指令串========================>|
特殊字符:
*(星號):任何時刻都接受的意思。(0 12 * * * 無論何月何日的星期幾的12:00都執行指令)
, (逗號):分割時間段。(0 3,6 * * * 3:00和6:00時就會執行指令)
- (減號):一段時間範圍。(20 8-12 * * * 8點到12點之間每個小時的20分鐘都執行指令)
/num (斜線): num代表數字,即每隔num單位間隔的意思。 (*/5 * * * * 每隔五分鐘執行一次指令)