Crontab定时执行任务,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.可以通过同样的方式监测到任务在重复执行

 

相关内容