crontab任务调度
首页->学习资料->linux教程->linux基础 关键词: 发布时间:2016-02-25 16:00:00 浏览次数:1515

所谓的任务调度与windows里面的计划任务类似

windows里面是在一个bat文件(批处理文件)中写好一些dos命令,然后在计划任务中将其添加进来,设置其何时执行即可;


在linux中用conrtab可以实现类似的功能;

1、查看当前有哪些任务调度?

[root@localhost hurong]# crontab -l
no crontab for root
由于还没有写过计划调度,这里查询出来的是没有


2.1、如果新建任务调度?
[root@localhost hurong]# crontab -e
回车后即进入crontab

用vi编辑器输入

* * * * * date >> /hurong/home/crontabtest

保存,退出

注意空格不能少,*号、date、>>之间,另外>>表示追加 >表示覆盖

再次查看可以看到已添加好了:

[root@localhost hurong]# crontab -l
* * * * * date >> /home/hurong/crontabtest
[root@localhost hurong]#
过一分钟后查看:

[root@localhost hurong]# pwd
/home/hurong
[root@localhost hurong]# ls
crontabtest  Documents  Music     Public     Videos
Desktop      Downloads  Pictures  Templates
[root@localhost hurong]#

看到确实生成了一个crontabtest的文件,过几分钟打开它,可以看到每隔一分钟系统的任务调度将系统时间写入到crontabtest文件中了,内容如下:

[root@localhost hurong]# cat crontabtest
Fri Feb 26 07:57:02 PST 2016
Fri Feb 26 07:58:01 PST 2016
Fri Feb 26 07:59:01 PST 2016
Fri Feb 26 08:00:01 PST 2016
Fri Feb 26 08:01:01 PST 2016
Fri Feb 26 08:02:01 PST 2016
[root@localhost hurong]#

2.2、更好的添加方式

上面的添加任务调度的方式虽然也可以用,但是如何有多人,很多个不同任务,全部写在里面就管理了,这时候可以写一个可执行文件(shell编程)然后再用任务调度这个shell程序,在shell里面可以用if else等进行条件判断,方便管理;

当前路径:

[root@localhost hurong]# pwd
/home/hurong
先建立一个testshell.sh文件(.sh文件即为linux里的可执行文件)

[root@localhost hurong]# vi testshell.sh

然后在其中输入如下内容,表示这个.sh文件被调用时将系统日期写入到/home/hurong/shelldate中:

date >> /home/hurong/shelldate
然后写任务调度:

[root@localhost hurong]# crontab -e
在其中输入如下内容,表示每分钟执行一次/home/hurong/testshell.sh:

* * * * * /home/hurong/testshell.sh

即终看到生成了我们需要的文件shelldate:

[root@localhost hurong]# ls
crontabtest  Documents  Music     Public     Templates     Videos
Desktop      Downloads  Pictures  shelldate  testshell.sh
[root@localhost hurong]#
打开它,里面的内容类似于:

[root@localhost hurong]# cat shelldate
Fri Feb 26 08:14:01 PST 2016
Fri Feb 26 08:15:01 PST 2016
Fri Feb 26 08:16:01 PST 2016
Fri Feb 26 08:17:01 PST 2016
Fri Feb 26 08:18:01 PST 2016
Fri Feb 26 08:19:01 PST 2016
Fri Feb 26 08:20:01 PST 2016
Fri Feb 26 08:21:01 PST 2016
[root@localhost hurong]#


备注:上面的* * * * *表示的是时间,精确度最小为分,全是*表示每分钟执行一次:

* * * * *依次对应 分 时 日 月 星期


3、如何删除?

crontab -r

-r即remove



crontab文件的一些例子:

30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启apache


[root@iZwz99m0qp1swglw9om6zxZ ~]# crontab -l
# .---------------- 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
# | | | | |

#每天从redis更新一次文章浏览次数到数据库
59 * * * * /wwwroot/github_www_hurong/hurong.sh

# 一分钟一次
#*/1 * * * * curl https://apibook.tnaot.com/grab_all_trips_data
*/1 * * * * /root/tnaot.sh
[root@iZwz99m0qp1swglw9om6zxZ ~]# 
[root@iZwz99m0qp1swglw9om6zxZ ~]# 
[root@iZwz99m0qp1swglw9om6zxZ ~]# 
[root@iZwz99m0qp1swglw9om6zxZ ~]# cat tnaot.sh 
#!/bin/bash
step=5 #间隔的秒数,不能大于60  
for (( i = 0; i < 60; i=(i+step) )); do
    $(curl  'https://apibook.tnaot.com/grab_all_trips_data')
    sleep $step
done
exit 0
[root@iZwz99m0qp1swglw9om6zxZ ~]#


赞:(0)
踩:(0)
相关文章
win7中将文件拷贝到虚拟机linux下
linux下切换root用户
linux目录结构
为linux指定运行级别
linux下vi编辑器的使用
linux下安装java
linux下安装tomcat
linux下安装mysql
linux下安装五笔输入法
linux下在终端命令行中显示中文
热门文章
win7中将文件拷贝到虚拟机linux下
phpexcel设置行高及列宽,背景颜色,
rabbitmq无法启动
intellij idea不显示git push按钮
php7中使用mongodb的aggregate进行
centos7.4 64位下swoole安装及配置
laravel页面静态化的方法
navicate连接mycat报1184错误
单点登录sso原理及php实现方式及de
devops-jenkins容器为pending状态
好评文章
phpexcel设置行高及列宽,背景颜色,
php7中使用mongodb的aggregate进行
intellij idea打开文件所在文件夹
windows下使用MongoDB Compass Com
win7中将文件拷贝到虚拟机linux下
laravel 中悲观锁 & 乐观锁的使用
单点登录sso原理及php实现方式及de
navicate连接mycat报1184错误
rabbitmq无法启动
laravel整合dingo/api方法步骤:jwt
标签
rabbitmq mysql备份 elasticsearch golang swoole
我的项目
【github】www.github.com/hurong241
【码云】gitee.com/hu_rong/projects
【docker hub】hub.docker.com/repositories/hurong241
【packagist】packagist.org/users/hurong241/packages
站点信息
建站时间:2011年
文章数:607篇
浏览数:932041
粤ICP备18028092号-1  微信:hurong241