1. 程式人生 > >Linux下新增任務計劃,計劃內容以sh檔案表示

Linux下新增任務計劃,計劃內容以sh檔案表示

sh檔案內容:
#/bin/sh
DATE=`date +%Y%m%d`
cd /your/log/path/
for f in *; do
cp $f  /path/to/save/$DATE_$f;
done


記得要給這個檔案加可執行許可權: chmod a+x   xxx.sh


for f 中的f 是會換成當前檔案的。


#/bin/sh
cd /your/log/path/
for f in *; do
  cp $f  /path/to/save/`date +%Y%m%d`_$f;
done
#這句會刪除當前目錄中10*24小時之前建立的檔案,就是10天前的。
find . -ctime +10 -exec rm -f {} \;
 要注意`date +%Y%m%d` 兩邊是反引號,就是鍵盤TAB上面1前面那個鍵。不要打錯。






加入到計劃任務是crontab -e  
然後就像使用vi一樣加入一行 (每天的2點01分執行)
01 02 * * * /path/to/your/xxx.sh
儲存退出。然後如果是RH系統最好重啟一下crond 服務,就OK了。

命令crontab -e 開啟的檔案如下:

0 * * * * /usr/sbin/ntpdate time.windows.com

*/10 * * * * (/usr/bin/php /home/monitor/hddusage.php)
0 */8 * * * (/usr/bin/php /home/monitor/hddhealth.php)

# 測試定期轉存訪問日誌檔案
1 0 * * * (/bin/bash /data02/xnou/binlog/rotate_nginx_log.sh >> /data02/xnou/binlog/task.log 2>&1 )




五、使設定生效
設定完成後,重啟cron即可使設定的計劃任務定時執行了,重啟命令如下:
有些linux系統是service cron restart
crond服務的重啟命令是
service cron stop
service cron start吧






重啟命令:service crond restart
檢視任務狀態命令
[
[email protected]
binlog]# service crond status
crond (pid  1332) 正在執行...






在運維行業裡流行著這麼一句話:能自動完成的,絕不手工去操作;這就涉及到Linux下的計劃任務crond 的設定,下面介紹crond 是如何設定的。


方法1:編輯/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
# | | | | |
# * * * * * command to be executed


格式說明:


第1列 分鐘1~59
第2列 小時1~23(0表示子夜)
第3列 日1~31
第4列 月1~12
第5列 星期0~6(0表示星期天)
第6列 要執行的命令


 


例子:


01 * * * * ntpdate time.windows.com


上面的例子表示每小時01分同步一次系統時間。


 


59 23 * * * /etc/init.d/httpd restart
上面的例子表示每晚的23:59重啟apache。


45 4 1 * * /etc/init.d/httpd restart


上面的例子表示每月1日的4:45分重啟apache。




45 4 1,10,22 * * /etc/init.d/httpd restart
上面的例子表示每月1、10、22日的4 : 45重啟apache。


10 1 * * 6,0 /usr/local/apache/bin/apachectl restart
上面的例子表示每週六、週日的1 : 10重啟apache。


0,30 18-23 * * * /usr/local/apache/bin/apachectl restart
上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重啟apache。


0 23 * * 6 /usr/local/apache/bin/apachectl restart
上面的例子表示每星期六的晚上11 : 00 pm重啟apache。


* */1 * * * /usr/local/apache/bin/apachectl restart
每一小時重啟apache


* 23-7/1 * * * /usr/local/apache/bin/apachectl restart
晚上11點到早上7點之間,每隔一小時重啟apache


0 11 4 * mon-wed /usr/local/apache/bin/apachectl restart
每月的4號與每週一到週三的11點重啟apache


0 4 1 jan * /etc/init.d/httpd restart
一月一號的4點重啟apache


 


方法2:crontab -e

這命令可以開啟計劃任務檔案,並在檔案裡新增任務


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)

0 * * * * /usr/sbin/ntpdate time.windows.com

*/10 * * * * (/usr/bin/php /home/monitor/hddusage.php)
0 */8 * * * (/usr/bin/php /home/monitor/hddhealth.php)

# remove expired files in '/tmp' directory.
0 1 * * * ( find /tmp/ -mtime +1 -exec rm -rf {} \; )

*/30 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checkStatusQueueTask.php)
15 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checkEncodeTask.php)
10 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checksms.php)

30,50 * * * * (/bin/sh /home/app/polyv/release-current/webapp/WEB-INF/sh/runjava.sh com.cc.ovp.web.stat.MRTask >> /dev/null 2>&1)

30 8 * * * (wget http://misc.polyv.net:8080/send_expiration_email)

59 5 * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/kw_flow_date.php)
22,42,58 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/kw_flow.php)

# 刪除最後修改時間為1天前的源視訊檔案
30 1 * * * ( find /data/htmlfile/?/video_source -mtime +1 -type f -exec rm -f {} \; )
# 硬碟監控
#*/10 * * * * /usr/bin/php /data/syssh/alert.php
*/10 * * * * /data/syssh/killffmpeg.sh
* * * * * (find /tmp/ -name "*.flv" -mmin +600 -exec rm -rf {} \;)
* * * * * (find /tmp/ -name "*.mp4" -mmin +600 -exec rm -rf {} \;)
* * * * * (find /data/htmlfile/?/video_target -mmin +200 -type f -exec rm -f {} \;)
* * * * * (find /data/htmlfile/?/video_source -mmin +200 -type f -exec rm -f {} \;)
#10 * * * * (>/data/mysql/data/log_error.err)

04 18 * * * (/bin/bash /data/nginx/sbin/cut-log-school.sh)


# 測試定期轉存訪問日誌檔案
1 0 * * * (/bin/bash /data02/xnou/binlog/rotate_nginx_log.sh >> /data02/xnou/binlog/task.log 2>&1 )

*/5 * * * * ( find /data/htmlfile/video_image/ -maxdepth 2 -type d -user root -exec chown -R web:web {} \; )
*/5 * * * * ( find /data/htmlfile/*/video_source/ -maxdepth 1 -type d -user root -exec chown -R web:web {} \; )
*/5 * * * * (/bin/sh /home/app/polyv/release-current/webapp/WEB-INF/sh/runjava.sh "com.ovp.test.TestAllVideoJson")

# 無限極首頁靜態化
*/5 * * * * ( wget "http://v.infinitus.com.cn/wxj/home" -O "/home/workspace/wuxianji/target/wuxianji/index.html" 2>&1 )




 


引數很簡單,這幾個單詞相信大家一看就明白了,格式跟方法1講的一樣,需要注意的是 crontab -e 寫的是使用者自己的計劃任務,檔案存放在以下目錄