天天看點

linux centos crontab定時器

這裡的定時器是centos7.2自帶,并且應該是設定了開機自動啟動,本人進伺服器就直接輸入定時器狀态查詢語句,結果顯示已經啟動。

[root@**** ~]# cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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
           

檢視定時器狀态:service crond status || systemctl status crond.service (兩種方式都可以)

[root@**** ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-12-11 00:03:53 CST; 2 months 27 days ago
 Main PID: 470 (crond)
   CGroup: /system.slice/crond.service
           └─470 /usr/sbin/crond -n

Dec 11 00:03:53 **** systemd[1]: Started Com...
Dec 11 00:03:53 **** systemd[1]: Starting Co...
Dec 11 00:03:53 **** crond[470]: (CRON) INFO...
Dec 11 00:03:53 **** crond[470]: (CRON) INFO...
Hint: Some lines were ellipsized, use -l to show in full.
[root@**** ~]# 
           

啟動定時器:service crond start || systemctl start crond.service

重新開機定時器:service crond restart || systemctl restart crond.service

關閉定時器:service crond stop || systemctl stop crond.service

重新加載:service crond reload || systemctl reload crond.service

正題:定時器操作

先建立一個檔案,測試定時器是否建立成功使用

[root@**** ~]# vim /home/www/test.txt
           

輸入内容 text  儲存退出

檢視定時器:crontab -l(目前使用者)|| crontab -l -u root (指定使用者);下面内容表示root使用者沒有建立定時器

[root@**** ~]# crontab -l
no crontab for root
[root@**** ~]# 
           

建立定時器:crontab -e (運作此指令後是直接編輯内容,同vim)

編輯完任務後建議重載一遍定時器服務,防止定時任務不生效(service crond reload || systemctl reload crond.service)

輸入以下内容儲存并退出(内容解釋:每 2 分鐘向 /home/www/test.txt 檔案中追加 testCrontab内容)

*/2 * * * * echo testCrontab >> /home/www/test.txt
           
[root@**** ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@**** ~]# 
           

等幾分鐘後檢視内容是否追加成功(向test.txt檔案追加了4次内容,即離建立定時器已有8分鐘之久,并表示定時器建立成功)

[root@**** ~]# cat /home/www/test.txt 
test
testCrontab
testCrontab
testCrontab
testCrontab
[root@**** ~]# 
           

再次檢視定時器:

[root@**** ~]# crontab -l
*/2 * * * * echo testCrontab >> /home/www/test.txt
[root@**** ~]# 
           

删除定時器:crontab  -r

[root@**** ~]# crontab -r
[root@**** ~]# 
           

再次檢視定時器:

[root@**** ~]# crontab -l
no crontab for root
[root@**** ~]# 
           

定時器内容解析:* * * * * command(分 時 日 月 周 指令)

  • 分:每小時的第幾分鐘(0-59)
  • 時:每天的幾時(0-23)
  • 日:每月的第幾天(1-31)
  • 月:每年的第幾月(1-12)
  • 周:每周的第幾天(0-6)周日為0
  • 指令:你要執行的内容

tips:

(*)代表任何時間;

(,)代表一個不連續的時間;

(-)代表連續的時間範圍

(*/特定的值)代表每隔一段時間執行

舉例:

*/2 * * * * echo testCrontab >> /home/www/test.txt (每2分鐘向test.txt檔案追加testCrontab内容)

30 7 * * 1-5 command(每周1至周5的早上7時30分執行)

*/15 23 31 12 * command(每年的12月31号晚上23時開始執行,間隔15分鐘執行一次,共執行4次)

15,45 * * * * command(每小時的15分,45分各執行一次)

附加:

方案一:将定時任務内容追加在  /etc/crontab 檔案後面,儲存退出後,重載/etc/crontab檔案(crontab /etc/crontab)

[root@**** ~]# vim /etc/crontab
           
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

# 定時任務
*/1 * * * * echo testCrontab >> /home/www/test.txt
           
[root@**** ~]# crontab /etc/crontab 
           

方案二:運作腳本

建立腳本檔案并給檔案執行權限,然後執行方案一步驟

[root@**** ~]# vim /home/www/test.sh 
           
#!/bin/bash
# chkconfig: 2345 10 90
# description: test

echo testCrontab >> /home/www/test.txt
           
[root@**** ~]# chmod +x /home/www/test.sh 
[root@**** ~]# vim /etc/crontab
           
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

# 定時任務
*/1 * * * * /home/www/test.sh
           
[root@**** ~]# crontab /etc/crontab 
           

結束

繼續閱讀