Linux-定時任務相關

1. cron服務【Ubuntu環境】

查看cron狀態

1
sudo  service cron status 

開啓cron

1
sudo /etc/init.d/cron start

關閉cron

1
sudo /etc/init.d/cron stop

重啓cron

1
sudo /etc/init.d/cron restart

2. crontab用法

crontab –e : 修改 crontab腳本 文件,如果文件不存在會自動創建。 
crontab –l : 顯示 crontab 文件。 
crontab -r : 刪除 crontab 文件。
crontab -ir : 刪除 crontab 文件前提醒用戶。

3. 腳本例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
每天早上6
0 6 * * * echo "Good morning." >> /tmp/test.txt //注意單純echo,從屏幕上看不到任何輸出,因爲cron把任何輸出都email到root的信箱了。
 
每兩個小時(第一個爲15,指明沒兩個小時的第15min中執行一次)
15 */2 * * * echo "Have a break now." >> /tmp/test.txt 
 
晚上11點到早上8點之間每兩個小時和早上八點
0 23-7/28 * * * echo "Have a good dream" >> /tmp/test.txt
 
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11
0 11 4 1-3 command line
 
11日早上4
0 4 1 1 * command line
 
每小時(第一分鐘)執行/etc/cron.hourly內的腳本
01 * * * * root run-parts /etc/cron.hourly
 
每天(凌晨402)執行/etc/cron.daily內的腳本
02 4 * * * root run-parts /etc/cron.daily
 
每星期(週日凌晨422)執行/etc/cron.weekly內的腳本
22 4 * * 0 root run-parts /etc/cron.weekly
 
每月(1號凌晨442)去執行/etc/cron.monthly內的腳本
42 4 1 * * root run-parts /etc/cron.monthly
 
注意:  "run-parts"這個參數了,如果去掉這個參數的話,後面就可以寫要運行的某個腳本名,而不是文件夾名。  
 
每天的下午4點、5點、6點的5 min、15 min、25 min、35 min、45 min、55 min時執行命令。
51525354555 161718 * * * command
 
每週一,三,五的下午300系統進入維護狀態,重新啓動系統。
00 15 * *135 shutdown -r +5
 
每小時的10分,40分執行用戶目錄下的innd/bbslin這個指令:
1040 * * * * innd/bbslink
 
每小時的1分執行用戶目錄下的bin/account這個指令:
1 * * * * bin/account
 
每天早晨三點二十分執行用戶目錄下如下所示的兩個指令(每個指令以;分隔):
203 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)  
 
每年的一月和四月,4號到9號的312分和355分執行/bin/rm -f expire.1st這個指令,並把結果添加在mm.txt這個文件之後(mm.txt文件位於用戶自己的目錄位置)。
12,553 4-91,4 * /bin/rm -f expire.1st$#@62;$#@62;mm.txt

4. 查看運行日誌

修改rsyslog

sudo vim /etc/rsyslog.d/50-default.conf

cron.*              /var/log/cron.log #將cron前面的註釋符去掉 

重啓rsyslog

sudo  service rsyslog  restart

查看crontab日誌

less  /var/log/cron.log  或者 tail -f

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章