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.可以通过同样的方式监测到任务在重复执行
转载于:https://www.cnblogs.com/lastword/p/5487015.html
Crontab定时执行任务相关推荐
- contOS crontab 定时执行python脚本
contOS crontab 定时执行python脚本 安装crontab yum install crontabs crontab语法 常用系统命令 ntsysv //查看系统开机启动项 chkco ...
- ubuntu 定时执行php文件,Ubuntu crontab 定时执行php脚本文件
什么是Cron和Crontab Unix和Linux系统的各个发行版本基本都支持Cron,Cron /Crontab允许我们在设定的时间自动执行或定时执行某个任务,如应用程序或脚本.更详细的概念和使用 ...
- Linux下Crontab定时执行命令
Linux下Crontab定时执行命令 目录 Crontab概述及安装 Crontab规则 常见例子 1. Crontab概述及安装 1. Crontab指令是Linux下执行定时任务的一个命令. 2 ...
- Linux系统上Crontab定时执行教程
Linux系统上Crontab定时执行教程 常用格式示例 每五分钟执行 */5 * * * * 每小时执行 0 * * * * 每天执行 0 0 * * * 每周执行 ...
- asp python 定时任务_Ubuntu使用crontab定时执行python脚本
转载其他博主的博客,记录过程,及使用中的一些问题 有时会遇到需要在linux的主机上定时的执行某个python脚本来处理事情的需求,这时可以考虑使用crontab来实现. 关于crontab的介绍,参 ...
- 部署scrapy爬虫到AWS Ubuntu 18.04,用crontab定时执行
本文介绍了如何把windows下面的scrapy脚本部署到AWS Ubuntu上,并使用crontab定时执行. 我的scrapy爬虫原来部署在windows服务器,爬虫每个小时从几个字幕网站爬字幕. ...
- Linux Crontab定时执行脚本出错,但手动执行脚本正常原因及解决方案
Linux Crontab定时执行脚本出错,但手动执行脚本正常原因及解决方案 实际开发场景 需要开发一个Flink监控程序,初步使用shell脚本进行监控,如果发现失败了,则自动重新运行Flink命令 ...
- crontab定时执行python脚本_linux下使用crontab定时执行python脚本
工作中遇到需要定时每周执行所写的python脚本,本来想使用python的定时任务框架,结果网上发现linux中提供crontab可以执行定时任务,省了好多代码,下面分享一下crontab 语法介绍 ...
- Linux下的crontab定时执行任务命令详解
在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间. cron的配置文件称为&q ...
- 如何使用Linux的Crontab定时执行PHP脚本的方法
来源:http://www.jb51.net/article/29136.htm 我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实 ...
最新文章
- 主机漏洞-SSL/TLS 受诫礼(BAR-MITZVAH)攻击漏洞(CVE-2015-2808)【原理扫描】-RC4密码套件
- Collection中的基本方法
- 每天一道LeetCode-----将数字集转成字母集,计算有多少种转换方式
- 前端笔记-thymeleaf获取及回显input标签type=time
- 内部排序的方法实验报告_python 十大经典排序算法
- 计算机视觉模型、学习和推理
- 拥有触觉分析能力,上交MIT获IROS 2020最佳论文奖
- SELinux系列(八)——SELinux默认安全上下文的查询和修改(semanage命令)
- java native方法_深入理解Java虚拟机
- python queue的用法_PYTHON多进程用法之四(Queue)
- 虚拟机未安装音频设备_如何使用虚拟音频设备录制PC的音频
- 编写递归算法,计算二叉树叶子结点的数目。
- 最新版UI视频解析单页源码附带超清接口
- 用python还需要stata吗_极冷知识点——Stata/Python/R自带数据导入(附代码)
- SLAM中边缘化与一致性
- 宏碁暗影骑士擎2022-重装系统-扬声器无声音
- 月薪25K-35K|格灵研究院招聘算法工程师、Java架构师
- 通过对json数据解析爬取虎牙直播数据并存入csv文件
- springboot中的事务
- 如何将PPT转换成Excel表格?教大家一种转换小妙招