ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

Crontab定时执行任务(2/2)

来源:网络整理     时间:2016-05-13     关键词:crontab,定时执行

本篇文章主要介绍了"Crontab定时执行任务",主要涉及到crontab,定时执行方面的内容,对于Linuxjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 最近接触到定时执行程序的需求,所以学习了解了一下crontab。本文首先介绍crontab的语法知识,然后做一个demo。一、crontab语法1.cronta...

1#helloworld.py2import time
3 with open('test.txt','w+') as f:
4    f.write(str(time.time()))
567#helloworld.sh8 cd /home/helloworld/
9 python /home/helloworld/helloworld.py

step1:执行crontab -e命令之后进入编辑模式,添加要执行的任务。

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

step2:启动crontab服务

step3:查看crontab日志

 1tail -f  /var/log/cron
 2#查看log文件
 3 May 1219:11:01 localhost CROND[12824]: (root) CMD (bash /home/helloworld/helloworld.sh)
 4 May 1219:11:01 localhost CROND[12823]: (root) MAIL (mailed 14 bytes of output but got status 0x007f#012)
 5 May 1219:12:01 localhost CROND[12865]: (root) CMD (bash /home/helloworld/helloworld.sh)
 6 May 1219:12:01 localhost CROND[12864]: (root) MAIL (mailed 14 bytes of output but got status 0x007f#012)
 7 May 1219:13:01 localhost CROND[12880]: (root) CMD (bash /home/helloworld/helloworld.sh)
 8 May 1219:13:01 localhost CROND[12879]: (root) MAIL (mailed 14 bytes of output but got status 0x007f#012)
 9 May 1219:14:01 localhost CROND[12895]: (root) CMD (bash /home/helloworld/helloworld.sh)
10 May 1219:14:01 localhost CROND[12894]: (root) MAIL (mailed 13 bytes of output but got status 0x007f#012)
1112#同时查看helloworld文件夹
13 ll -t
14#监测到
15 [root@localhost helloworld]# ll -t
16 总用量 1217 -rw-r--r-- 1 root root 13 5月  1219:11 test.txt
18 -rw-r--r-- 1 root root 59 5月  1216:45 helloworld.sh19 -rw-r--r-- 1 root root 94 5月  1216:41 helloworld.py
20 [root@localhost helloworld]# ll -t
21 总用量 1222 -rw-r--r-- 1 root root 13 5月  1219:12 test.txt
23 -rw-r--r-- 1 root root 59 5月  1216:45 helloworld.sh24 -rw-r--r-- 1 root root 94 5月  1216:41 helloworld.py
25 [root@localhost helloworld]# ll -t
26 总用量 1227 -rw-r--r-- 1 root root 13 5月  1219:13 test.txt
28 -rw-r--r-- 1 root root 59 5月  1216:45 helloworld.sh29 -rw-r--r-- 1 root root 94 5月  1216:41 helloworld.py
30 [root@localhost helloworld]# ll -t
31 总用量 1232 -rw-r--r-- 1 root root 12 5月  1219:14 test.txt
33 -rw-r--r-- 1 root root 59 5月  1216:45 helloworld.sh34 -rw-r--r-- 1 root root 94 5月  1216: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=/
 910 # For details see man4 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.可以通过同样的方式监测到任务在重复执行

以上就介绍了Crontab定时执行任务,包括了crontab,定时执行方面的内容,希望对Linuxjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_1040667_2.html

相关图片

相关文章