Crontab定時執行任務


最近接觸到定時執行程序的需求,所以學習了解了一下crontab。本文首先介紹crontab的語法知識,然后做一個demo。

一、crontab語法

1.crontab基本格式

 {minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}

2.crontab語法示例

 1 #在 12:01a.m運行
 2 1 0 * * * /root/bin/backup.sh 
 3 #每個工作日11:59p.m運行
 4 59 11 * * 1,2,3,4,5 /root/bin/backup.sh 
 5 59 11 * * 1-5 /root/bin/backup.sh 
 6 #每5分鍾運行一次命令 
 7 */5 * * * * /root/bin/check-status.sh 
 8 #每個月的第一天1:10p.m運行 
 9 10 13 1 * * /root/bin/full-backup.sh 
10 #每個工作日11p.m運行
11 0 23 * * 1-5 /root/bin/incremental-backup.sh

二、crontab實例

1.crontab的啟動和停止

1 service crond start/stop/restart

2.crontab -e方式添加crontab任務

1 crontab -e   #添加任務
2 crontab -l   #查看所有任務    
3 crontab -r   #刪除任務

准備工作:

vim /home/helloworld/helloworld.py

vim /home/helloworld/helloworld.sh

1 #helloworld.py
2 import time
3 with open('test.txt','w+') as f:
4     f.write(str(time.time()))
5 
6 
7 #helloworld.sh
8 cd /home/helloworld/
9 python /home/helloworld/helloworld.py

 

step1:執行crontab -e命令之后進入編輯模式,添加要執行的任務。

*/1 * * * * bash /home/helloworld/helloworld.sh

step2:啟動crontab服務

step3:查看crontab日志

 1 tail -f  /var/log/cron
 2 #查看log文件
 3 May 12 19:11:01 localhost CROND[12824]: (root) CMD (bash /home/helloworld/helloworld.sh)
 4 May 12 19:11:01 localhost CROND[12823]: (root) MAIL (mailed 14 bytes of output but got status 0x007f#012)
 5 May 12 19:12:01 localhost CROND[12865]: (root) CMD (bash /home/helloworld/helloworld.sh)
 6 May 12 19:12:01 localhost CROND[12864]: (root) MAIL (mailed 14 bytes of output but got status 0x007f#012)
 7 May 12 19:13:01 localhost CROND[12880]: (root) CMD (bash /home/helloworld/helloworld.sh)
 8 May 12 19:13:01 localhost CROND[12879]: (root) MAIL (mailed 14 bytes of output but got status 0x007f#012)
 9 May 12 19:14:01 localhost CROND[12895]: (root) CMD (bash /home/helloworld/helloworld.sh)
10 May 12 19:14:01 localhost CROND[12894]: (root) MAIL (mailed 13 bytes of output but got status 0x007f#012)
11 
12 #同時查看helloworld文件夾
13 ll -t
14 #監測到
15 [root@localhost helloworld]# ll -t
16 總用量 12
17 -rw-r--r-- 1 root root 13 5月  12 19:11 test.txt
18 -rw-r--r-- 1 root root 59 5月  12 16:45 helloworld.sh
19 -rw-r--r-- 1 root root 94 5月  12 16:41 helloworld.py
20 [root@localhost helloworld]# ll -t
21 總用量 12
22 -rw-r--r-- 1 root root 13 5月  12 19:12 test.txt
23 -rw-r--r-- 1 root root 59 5月  12 16:45 helloworld.sh
24 -rw-r--r-- 1 root root 94 5月  12 16:41 helloworld.py
25 [root@localhost helloworld]# ll -t
26 總用量 12
27 -rw-r--r-- 1 root root 13 5月  12 19:13 test.txt
28 -rw-r--r-- 1 root root 59 5月  12 16:45 helloworld.sh
29 -rw-r--r-- 1 root root 94 5月  12 16:41 helloworld.py
30 [root@localhost helloworld]# ll -t
31 總用量 12
32 -rw-r--r-- 1 root root 12 5月  12 19:14 test.txt
33 -rw-r--r-- 1 root root 59 5月  12 16:45 helloworld.sh
34 -rw-r--r-- 1 root root 94 5月  12 16:41 helloworld.py
35 [root@localhost helloworld]# 

3.通過vim /etc/crontab方式添加任務

 1 #查看/etc/crontab
 2 vim /etc/crontab
 3 
 4 #添加任務
 5 SHELL=/bin/bash
 6 PATH=/sbin:/bin:/usr/sbin:/usr/bin
 7 MAILTO=""
 8 HOME=/
 9 
10 # For details see man 4 crontabs 
11 # Example of job definition:
12 # .---------------- minute (0 - 59)
13 # |  .------------- hour (0 - 23)
14 # |  |  .---------- day of month (1 - 31)
15 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
16 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,w    ed,thu,fri,sat
17 # |  |  |  |  |
18 # *  *  *  *  * user-name command to be executed
19 */1 * * * * root bash /home/helloworld/helloworld.sh                   

4.可以通過同樣的方式監測到任務在重復執行

 


免責聲明!

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



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