標籤:at cron crond linux任務計劃
一、單次任務計劃
二、週期性任務計劃
三、轉載陳明乾的博文
一、單次任務計劃
命令:
batch:系統空閑時自動執行,不常用
at:可以使用相對時間、絕對時間或模糊時間,例如
相對時間:at now+3min,表示3分鐘後執行
絕對時間:at 11:30,表示在11:30執行
模糊時間:at noon | midnight | teatime,表示在對應模糊時間執行
------------------------------------------
at常用選項:
-l:查看等待執行的任務,同atq
-d: 刪除未執行的任務,同atrm
-f:指定指令檔
------------------------------------------
1、使用at計劃任務
[[email protected] SPECS]# at now+3min #3分鐘後執行的任務 at> cat /etc/passwd #任務1 at> cat /etc/shadow #任務2at> date #任務3at> ls /scripts/ #任務4at> <EOT> #結束Ctrl+d提交任務 job 3 at 2015-04-24 08:20 You have mail in /var/spool/mail/root #執行結果以郵件方式發送給任務發起者
2、查看郵件,使用命令mail
[[email protected] SPECS]# mail #使用mail命令查看郵件Heirloom Mail version 12.4 7/29/08. Type ? for help."/var/spool/mail/root": 3 messages #下列資訊最前面的數字1為郵件編號> 1 root Fri Apr 24 08:17 61/2298 "Output from your job " 2 root Fri Apr 24 08:18 15/524 "Output from your job " 3 root Fri Apr 24 08:20 62/2327 "Output from your job "& 1 #提示符&後輸入郵件編號,查看任務執行結果Message 1:From [email protected] Fri Apr 24 08:17:14 2015Return-Path: <[email protected]>X-Original-To: rootDelivered-To: [email protected]Subject: Output from your job 1To: [email protected]Date: Fri, 24 Apr 2015 08:17:14 +0800 (CST)From: [email protected] (root)Status: RO#資訊太多,這裡略去##################################################New mail has arrived. #提示有新郵件到達Loaded 1 new message N 4 root Fri Apr 24 19:29 31/571 "Output from your job "& q #輸入q斷行符號,退出mailHeld 4 messages in /var/spool/mail/root #告訴root使用者有4條資訊[[email protected] SPECS]#
3、查看等待執行的任務,使用命令at -l或atq
[[email protected] SPECS]# at -l62015-04-24 19:41 a root #數字6為任務編號[[email protected] SPECS]# atq62015-04-24 19:41 a root
4、刪除未執行的任務,使用命令at –d或atrm
[[email protected] SPECS]# at now+3min #建立一個任務計劃at> ls /varat> <EOT>job 7 at 2015-04-24 19:48[[email protected] SPECS]# at -l #查看待執行任務,任務編號為772015-04-24 19:48 a root[[email protected] SPECS]# date #查看當前系統時間Fri Apr 24 19:45:44 CST 2015[[email protected] SPECS]# at -d 7 #刪除未執行的任務,at -d #任務編號[[email protected] SPECS]# at -l #任務已刪除
5、計劃執行指令碼,使用命令at –f /path/to/scriptfile TIME
注:使用at命令,Ctrl+d提交任務出現如下提示,則證明計劃任務依賴的atd服務未啟動
Can‘t open /var/run/atd.pid to signal atd. No atd running?
查看atd服務狀態,並啟動
[[email protected] SPECS]# /etc/init.d/atd status #查看服務狀態atd is stopped[[email protected] SPECS]# /etc/init.d/atd start #啟用服務Starting atd: [ OK ][[email protected] SPECS]# /etc/init.d/atd status #服務已運行atd (pid 34529) is running...
二、週期性任務計劃
1、依賴服務:crond
[[email protected] SPECS]# service crond statuscrond (pid 1222) 正在運行...
2、crond設定檔
2.1、 系統crond設定檔及格式:/etc/crontab
[[email protected] ~]# cat /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=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 #星號後分別接使用者、任務命令
2.2、使用者crond設定檔及格式:/var/spool/cron/USERNAME
[[email protected] ~]# crontab -e #編輯目前使用者的crond設定檔no crontab for root - using an empty one #編輯後,儲存退出,提示為root使用者建立crond設定檔crontab: installing new crontab [[email protected] ~]# ls /var/spool/cron/ #使用者crond設定檔,檔案名稱同使用者名稱root[[email protected] ~]# cat /var/spool/cron/root # 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 #星號後接任務命令*/2 * * * * ls /var
注:系統預設PATH=/bin:/sbin:/usr/bin:/usr/sbin,計劃任務中使用的命令不在系統預設PATH中時,則使用絕對路徑;使用指令碼時,在指令碼中自行定義PATH
2.3、設定檔周期格式含義
支援萬用字元:
*:相應時間點的所有有效取值,例如
3 * * * * #每小時第3分3 4 * * * #每天的4點03分3 4 5 * * #每月5號的4點03分
,:離散取值,例如
3,15,27,39,51 * * * * #每12分鐘
/:定義指定取值範圍內的頻率,例如
*/2 * * * * #每2分鐘17 */3 * * * #每3小時的第17分
-:連續取值,例如
17 8-20/3 * * * #每天的8時-20時之間的每小時的第17分
以秒單位執行任務計劃:
方法1:
* * * * * sleep 30 && /usr/bin/unison webdata #每30秒
方法2:
* * * * * for i in 0 1 2; do some_job && sleep 15; done; some_job #每15秒,迴圈次數為(60/n-1)
* * * * * for i in {1..5}; do some_job && sleep 10; done; some_job #每10秒
注意:使用%時要加反斜線。例如:
1 2 3 * * touch ~/file_$(date +\%Y\%m\%d).txt
但使用單引號後也可以不再使用反斜線,例如:
1 2 3 * * touch ~/file_$(date ‘+%Y%m%d‘).txt
2.4、拒收郵件
方法1:設定檔中定義MAILTO=’’
方法2:將命令輸出資訊重新導向 &> /dev/null或> /dev/null 2>&1 #bash下,這兩個重新導向等價
3、crontab命令
常用選項:
-e:編輯任務計劃
-r:移除設定檔,刪除部分任務計劃使用-e編輯
-l:列出週期性任務
-u USERNAME: #管理員可用的選項,可結合選項-e,-r,-l編輯、刪除、查看其他使用者的任務
三、轉載陳明乾的博文
博主的歸納總結品質都很高,文章的知識結構劃分更容易閱讀和理解,謝謝哈!
單擊圖片後放大查看
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6B/EC/wKiom1U6A6yT2qMTAA3IacXS2Sc108.jpg" title="2033581_13699632174Uts.jpg" alt="wKiom1U6A6yT2qMTAA3IacXS2Sc108.jpg" />
本文出自 “Arvin Lau” 部落格,請務必保留此出處http://64314491.blog.51cto.com/2784219/1638062
Linux之任務計劃