Linux定時(shí)任務(wù)使用

.---------------- 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

minute:代表一小時(shí)內(nèi)的第幾分,范圍 0-59。
hour:代表一天中的第幾小時(shí),范圍 0-23。
mday:代表一個(gè)月中的第幾天,范圍 1-31。
month:代表一年中第幾個(gè)月,范圍 1-12。
wday:代表星期幾,范圍 0-7 (0及7都是星期天)。
who:要使用什么身份執(zhí)行該指令,當(dāng)您使用 crontab -e 時(shí),不必加此字段。
command:所要執(zhí)行的指令。

一般格式

* * * * *  who(用戶)  command(命令)

下面介紹創(chuàng)建定時(shí)任務(wù)的2種方法:

  • 方法一:
crontab -e 其中 root代表用戶 可不寫(xiě)

* * * * *   root   /xx/xx/xx/xxx.sh
  • 方法二:
在/etc/cron.d/目錄下創(chuàng)建 testCrontab 的文件

touch testCrontab

文件中編輯如下,其中 root代表用戶 是要寫(xiě)的

* * * * *  root  /xx/xx/xx/xxx.sh

下面列舉一些例子

# 每天早上6點(diǎn) 
0 6 * * * echo "Good morning." >> /tmp/test.txt //注意單純echo,從屏幕上看不到任何輸出,因?yàn)閏ron把任何輸出都email到root的信箱了。

# 每?jī)蓚€(gè)小時(shí) 
0 */2 * * * echo "Have a break now." >> /tmp/test.txt  

# 晚上11點(diǎn)到早上8點(diǎn)之間每?jī)蓚€(gè)小時(shí)和早上八點(diǎn) 
0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

# 每個(gè)月的4號(hào)和每個(gè)禮拜的禮拜一到禮拜三的早上11點(diǎn) 
0 11 4 * 1-3 command line

# 1月1日早上4點(diǎn) 
0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //如果出現(xiàn)錯(cuò)誤,或者有數(shù)據(jù)輸出,數(shù)據(jù)作為郵件發(fā)給這個(gè)帳號(hào) HOME=/ 

# 每小時(shí)(第一分鐘)執(zhí)行/etc/cron.hourly內(nèi)的腳本
01 * * * * root run-parts /etc/cron.hourly

# 每天(凌晨4:02)執(zhí)行/etc/cron.daily內(nèi)的腳本
02 4 * * * root run-parts /etc/cron.daily 

# 每星期(周日凌晨4:22)執(zhí)行/etc/cron.weekly內(nèi)的腳本
22 4 * * 0 root run-parts /etc/cron.weekly 

# 每月(1號(hào)凌晨4:42)去執(zhí)行/etc/cron.monthly內(nèi)的腳本 
42 4 1 * * root run-parts /etc/cron.monthly 

# 注意:  "run-parts"這個(gè)參數(shù)了,如果去掉這個(gè)參數(shù)的話,后面就可以寫(xiě)要運(yùn)行的某個(gè)腳本名,而不是文件夾名。   

# 每天的下午4點(diǎn)、5點(diǎn)、6點(diǎn)的5 min、15 min、25 min、35 min、45 min、55 min時(shí)執(zhí)行命令。 
5,15,25,35,45,55 16,17,18 * * * command line

# 每周一,三,五的下午3:00系統(tǒng)進(jìn)入維護(hù)狀態(tài),重新啟動(dòng)系統(tǒng)。
00 15 * * 1,3,5 shutdown -r +5

# 每小時(shí)的10分,40分執(zhí)行用戶目錄下的innd/bbslin這個(gè)指令: 
10,40 * * * * innd/bbslink 

# 每小時(shí)的1分執(zhí)行用戶目錄下的bin/account這個(gè)指令: 
1 * * * * bin/account

# 每天早晨三點(diǎn)二十分執(zhí)行用戶目錄下如下所示的兩個(gè)指令(每個(gè)指令以;分隔): 
20 3 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容