在
Linux
中,週期執行的任務通常由cron
這個守護進程來處理,能夠在無需人工干預的狀況下運行做業。php
service crond status
要使用crontab定時器工具,必需要啓動cron服務:服務器
service crond start 或者/etc/rc.d/init.d/crond start
chkconfig --level 35 crond on
查看是否已加入開機自啓動:
使用 chkconfig | grep crond
看在2 3 4 5
級別是否是oncrontab
默認就是開機啓動的,普通用戶要有sudo
的權限才能設置開機啓動。編輯器
編輯 crontab
:工具
crontab -e
基本格式 :oop
* * * * * command
分 時 日 月 周 命令spa
-e : 執行文字編輯器來編輯crontab,內定的文字編輯器是VI.net
00 00 * /usr/bin/php /xxx/Timer.php 天天凌晨執行PHP腳本日誌
設置後:crontab: installing new crontab
code
切切:
必定要寫對/usr/bin/php
路徑,就由於這個問題我搗鼓整整一天(淚),能夠用which php
查看(點贊)blog
-r : 刪除目前的crontab -l : 列出目前的crontab(查看專用) -i : 會和-r 配合使用,在刪除當前的crontab時詢問,輸入y 則刪除
注意:crontab
是分用戶的,以誰登陸就會編輯到誰的crontab
。
"*"表明全部的取值範圍內的數字。特別要注意哦! "/"表明每的意思,如"*/5"表示每5個單位 "-"表明從某個數字到某個數字 ","分散的數字
crontab文件的使用示例:
30 21 * * * 表示每晚的21:30 45 4 1,10,22 * * 表示每個月一、十、22日的4 : 45 10 1 * * 6,0 表示每週6、週日的1 : 10 0,30 18-23 * * * 表示在天天18 : 00至23 : 00之間每隔30分鐘 0 23 * * 6 表示每星期六的11 : 00 pm * */1 * * * 每一小時 * 23-7/1 * * * 晚上11點到早上7點之間,每隔一小時 * 8,13 * * 1-5 從週一到週五的上午8點和下午1點 0 11 4 * mon-wed 每個月的4號與每週一到週三的11點 0 4 1 jan * 一月一號的4點
排查緣由步驟以下:
首先,確認服務器是否開啓定時任務計劃服務,只有root
用戶才能對crond
服務進行開啓和關閉
[root@mimvp-bj script]# service crond status crond is stopped [root@mimvp-bj script]# service crond start Starting crond: [ OK ] [root@mimvp-bj script]# service crond status crond (pid 24577) is running…
請確保crond
狀態爲 is running…
若是crond
狀態爲 crond is stopped
,則定時任務服務不會生效故沒法執行
查看動態日誌:
tail -f /var/log/cron
使用 crontab -e 與 直接編輯 /etc/crontab 的區別
crontab 每分鐘、每小時、天天、每週、每個月、每一年定時執行