1.寫腳本
最簡單的 寫如下代碼
#!/bin/sh
A
B
C
1.每個命令之間用;隔開
說明:各命令的執行給果,不會影響其它命令的執行。換句話說,各個命令都會執行,
但不保證每個命令都執行成功。
2.每個命令之間用&&隔開
說明:若前面的命令執行成功,才會去執行后面的命令。這樣可以保證所有的命令執行完畢后,執行過程都是成功的。
例如:cat /etc/redhat-release && yum -y update && yum list | grep installed > installed.txt 三個命令
3.每個命令之間用||隔開
說明:||是或的意思,只有前面的命令執行失敗后才去執行下一條命令,直到執行成功
一條命令為止。
保存為test.sh
然后添加執行權限chmod +x test.sh
然后執行該腳本./test.sh
{EG:#!/bin/bash
echo “I love you !” >> /home/luogen30/Desktop/t.txt
}
2.編輯定時任務
crontab –e
30 7 * * * root /root/shell/rule1.sh >> /root/shell/filename.log 2>&1
(執行腳本script.sh時將錯誤輸出2以及標準輸出1都一起以附加寫方式導入logfile文件。
即使多次執行腳本,之前的log也仍然存在。)
為每天7點半執行,并且導入日志到后面
/etc/init.d/cron start //啟動服務
/etc/init.d/cron stop //關閉服務
/etc/init.d/cron restart //重啟服務
/etc/init.d/cron reload //重新載入配置
/etc/init.d/cron status //查看服務狀態
查看已經編寫的定時任務:
在命令終端輸入:crontab -l
在命令終端 執行:
cron restart重啟定時任務.
service crond restart
摘錄
crontab文件的格式:
minute hour day month weekday username command
minute:分,值為0-59
hour:小時,值為1-23
day:天,值為1-31
month:月,值為1-12
weekday:星期,值為0-6(0代表星期天,1代表星期一,以此類推)
username:要執行程序的用戶,一般設置為root
command:要執行的程序路徑(設置為絕對路徑)例如:/home/www.osyunwei.com/osyunwei.sh
附:crontab規則詳細實例
1、每天6:00執行
0 6 * * * root /home/www.osyunwei.com/osyunwei.sh
2、每周六凌晨4:00執行
0 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh
3、每周六凌晨4:05執行
5 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh
4、每周六凌晨4:15執行
15 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh
5、每周六凌晨4:25執行
25 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh
6、每周六凌晨4:35執行
35 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh
7、每周六凌晨5:00執行
5 * * 6 root /home/www.osyunwei.com/osyunwei.sh
8、每天8:40執行
40 8 * * * root /home/www.osyunwei.com/osyunwei.sh
9、每天8:30執行
30 8 * * * root /home/www.osyunwei.com/osyunwei.sh
10、每周一到周五的11:41開始,每隔10分鐘執行一次
41,51 11 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
1-59/10 12-23 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
11、在每天的10:31開始,每隔2小時重復一次
31 10-23/2 * * * root /home/www.osyunwei.com/osyunwei.sh
12、每天15:00執行
0 15 * * * root /home/www.osyunwei.com/osyunwei.sh
13、每天的10:30開始,每隔2小時重復一次
30 10-23/2 * * * root /home/www.osyunwei.com/osyunwei.sh
14、每天15:30執行
30 15 * * * root /home/www.osyunwei.com/osyunwei.sh
15、每天17:50執行
50 17 * * * root /home/www.osyunwei.com/osyunwei.sh
16、每天8:00執行
0 8 * * * root /home/www.osyunwei.com/osyunwei.sh
17、每天18:00執行
0 18 * * * root /home/www.osyunwei.com/osyunwei.sh
18、每天8:30執行
30 8 * * * root /home/www.osyunwei.com/osyunwei.sh
19、每天20:30
30 20 * * * root /home/www.osyunwei.com/osyunwei.sh
20、每周一到周五2:00
0 2 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
21、每周一到周五9:30
30 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
22、每周一到周五8:00,每周一到周五9:00
0 8,9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
23、每天23:59
59 23 * * * root /home/www.osyunwei.com/osyunwei.sh
24、每周六23:59
59 23 * * 6 root /home/www.osyunwei.com/osyunwei.sh
25、每天0:30
30 0 * * * root /home/www.osyunwei.com/osyunwei.sh
26、每周一到周五9:25到11:35之間、13:00到15:00之間,每隔10分鐘運行一次
25,35,45,55 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
5-59/10 10 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
5,15,25,35 11 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
*/10 13-15 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
27、每周一到周五8:30、8:50、9:30、10:00、10:30、11:00、11:30、13:30、14:00、14:30、5:00分別執行一次
30,50 8 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
30 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
*/30 10-11 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
30 13 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
0,30 14-15 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
28、每天23:50執行
50 23 * * * root /home/www.osyunwei.com/osyunwei.sh
29、每天10:00、16:00執行
0 10,16 * * * root /home/www.osyunwei.com/osyunwei.sh
30、每天5:30執行
30 5 * * * root /home/www.osyunwei.com/osyunwei.sh
31、每周一到周五9:30執行
30 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
32、每周一到周五13:00執行
0 13 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh
33、每天7:51執行
51 7 * * * root /home/www.osyunwei.com/osyunwei.sh
34、每天7:53、12:40分別執行一次
53 7 * * * root /home/www.osyunwei.com/osyunwei.sh
40 12 * * * root /home/www.osyunwei.com/osyunwei.sh
35、每天7:55執行
55 7 * * * root /home/www.osyunwei.com/osyunwei.sh
36、每天8:10、16:00、20:00分別執行一次
10 8 * * * root /home/www.osyunwei.com/osyunwei.sh
0 16 * * * root /home/www.osyunwei.com/osyunwei.sh
0 20 * * * root /home/www.osyunwei.com/osyunwei.sh
37、每天7:57、8:00分別執行一次
57 7 * * * root /home/www.osyunwei.com/osyunwei.sh
0 8 * * * root /home/www.osyunwei.com/osyunwei.sh
這里放置我之前服務器運行的一些計劃任務
05 13 * * * /scripts/test.sh >/opt/crontablog.txt
30 06 * * * /shell/end.sh >/opt/crontablog.txt
備注:有幾次我計劃任務上寫root用戶,卻發現執行不了