(總結)CentOS Linux使用crontab運行定時任務詳解


安裝crontab:
yum install crontabs

說明:
/sbin/service crond start //啟動服務
/sbin/service crond stop //關閉服務
/sbin/service crond restart //重啟服務
/sbin/service crond reload //重新載入配置

 

查看crontab服務狀態:service crond status

手動啟動crontab服務:service crond start

編輯crontab定時任務: crontab -e (如果想取消定時任務,可以在語句前面加上#,或者直接刪除該語句)

查看crontab定時任務: crontab -l

查看crontab服務是否已設置為開機啟動,執行命令:ntsysv

加入開機自動啟動:
chkconfig –level 35 crond on

1,crontab命令

功能說明:設置計時器。

語  法:crontab [-u <用戶名稱>][配置文件] 或 crontab [-u <用戶名稱>][-elr]

補充說明:cron是一個常駐服務,它提供計時器的功能,讓用戶在特定的時間得以執行預設的指令或程序。只要用戶會編輯計時器的配置文件,就可以使 用計時器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command

參  數:
-e  編輯該用戶的計時器設置。
-l  列出該用戶的計時器設置。
-r  刪除該用戶的計時器設置。
-u<用戶名稱>  指定要設定計時器的用戶名稱。

2,crontab 格式

基本格式 :
* *  *  *  *  command
分 時 日 月 周  命令

第1列表示分鍾1~59 每分鍾用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列 表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要運行的命令

# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed

crontab文件的一些例子:

30 21 * * * /etc/init.d/nginx restart
每晚的21:30重啟 nginx。

45 4 1,10,22 * * /etc/init.d/nginx restart
每月1、 10、22日的4 : 45重啟nginx。

10 1 * * 6,0 /etc/init.d/nginx restart
每周六、周日的1 : 10重啟nginx。

0,30 18-23 * * * /etc/init.d/nginx restart
每天18 : 00至23 : 00之間每隔30分鍾重啟nginx。

0 23 * * 6 /etc/init.d/nginx restart
每星期六的11 : 00 pm重啟nginx。

* */1 * * * /etc/init.d/nginx restart
每一小時重啟nginx

* 23-7/1 * * * /etc/init.d/nginx restart
晚上11點到早上7點之間,每 隔一小時重啟nginx

0 11 4 * mon-wed /etc/init.d/nginx restart
每月的4號與每周一到周三 的11點重啟nginx

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

*/30 * * * * /usr/sbin/ntpdate 210.72.145.20
每半小時同步一下時間


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM