Linux  Crontab调度_linux




概述



Linux crontab是用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond 命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。注意:新创建的 cron 任务,不会马上执行,至少要过 2 分钟后才可以,当然你可以重启 cron 来马上执行。

linux 任务调度的工作主要分为以下两类


  • 系统执行的工作:系统周期性所要执行的工作,如:备份系统数据、清理缓存
  • 个人执行的工:某个用户定期要做的工作,例如每隔10分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置



Crontab 服务启动停止





service crond start     # 启动服务
service crond stop # 关闭服务
service crond restart # 重启服务
service crond reload # 重新载入配置
service crond status # 查看crontab服务状态


# 可以使用下面的命令加入开机启动
chkconfig --level 345 crond on



Crontab 命令



crontab [-u user] file
crontab [ -u user ] [ -i ] { -e | -l | -r }


参数

描述

-u user

用于设定某个用户的crontab服务;
这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设定自己的时程表

file

file为命令文件名,表示将file作为crontab的任务列表文件并载入crontab

-e

编辑某个用户的crontab文件内容,如不指定用户则表示当前用户

-l

显示某个用户的crontab文件内容,如不指定用户则表示当前用户

-r

从/var/spool/cron目录中删除某个用户的crontab文件

-i

在删除用户的crontab文件时给确认提示



Crontab 时间格式




# Example of job definition:
# .---------------- 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
# | | | | |
# * * * * * user-name command to be executed


Linux  Crontab调度_linux_02



Crontab 每个星号 “*” 可用的格式




  • 星号(*):通配符匹配,代表所有可能的值。
  • 逗号(,):可以用逗号隔开的值指定一个列表范围,例如:“1,2,5,7,8,9”。
  • 中杠(-):可以用整数之间的中杠表示一个整数范围,例如:“2-6”表示“2,3,4,5,6”。
  • 正斜线(/):可以用正斜线指定时间的间隔频率,例如:“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如:*/10,如果用在minute字段,表示每十分钟执行一次。



Crontab 相关目录含义



  • /var/spool/cron/


目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名


Linux  Crontab调度_环境变量_03


  • /etc/crontab


这个文件负责调度各种管理和维护任务(同Crontab 时间格式主题)



# Example of job definition:
# .---------------- 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
# | | | | |
# * * * * * user-name command to be executed


  • /etc/cron.d/


这个目录用来存放任何要执行的crontab文件或脚本

Linux  Crontab调度_环境变量_04


  • ls /etc/cron.*


把脚本以上目录中,让它每小时/天/星期、月执行一次。




/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly


Linux  Crontab调度_环境变量_05




输入输出重定向



Linux中使用0-3作为文件描述符来表示标准流。



名称

类型

文件描述符

操作

stdin 标准输入

standard input

0

​<​,​<<​

stdout 标准输出

standard output

1

​>​,​>>​

stderr 标准错误输出

standard error output

2

​2>​,​2>>​



* * * * * sh /home/work/mytest.sh >> /data/mytest.log 2>&1

在上面的定时任务脚本中,​mytest.sh​后面的​>>​表示将输出追加到​mytest.log​文件中,​2>&1​表示标准错误输出重定向等同于标准输出。



脚本无法执行问题



如果我们使用 crontab 来定时执行脚本,无法执行,但是如果直接通过命令(如:./test.sh)又可以正常执行,这主要是因为无法读取环境变量的原因。


解决方法


  • 所有命令需要写成绝对路径形式,如: 



/usr/local/bin/docker
  • 在 shell 脚本开头使用以下代码:



#!/bin/sh


  • ​ /etc/crontab​ 中添加环境变量,在可执行命令之前添加命令 . ​/etc/profile;/bin/sh​使得环境变量生效,例如:



* * * * * . /etc/profile;/bin/sh /data/test.sh


例子



每一分钟执行一次duoduo(因cron默认每1分钟扫描一次,因此全为*即可)


* * * * * duoduo


每小时的第3和第15分钟执行duoduo


3,15  * * * *  duoduo


每天上午8-11点的第3和15分钟执行duoduo


3,15  8-11 * * *  duoduo


每隔2天的上午8-11点的第3和15分钟执行duoduo


3,15 8-11 */2 * *  duoduo


每个星期一的上午8点到11点的第3和第15分钟执行duoduo


3,15  8-11 * *  1 duoduo


每晚的21:30重启duoduo 


30 21 * * *  /etc/init.d/duoduo restart


每月1、10、22日的4 : 45重启duoduo


45 4 1,10,22 * *  /etc/init.d/duoduo restart


每周六、周日的1 : 10重启duoduo


10 1 * * 6,0  /etc/init.d/duoduo restart


每天18 : 00至23 : 00之间每隔30分钟重启duoduo


0,30 18-23 * * *  /etc/init.d/duoduo restart


每一小时重启duoduo


*/1 * * *  /etc/init.d/duoduo restart


晚上11点到早上7点之间,每隔一小时重启duoduo


23-7/1 * * *  /etc/init.d/duoduo restart


每月的4号与每周一到周三的11点重启duoduo


0 11 4 * mon-wed  /etc/init.d/duoduo restart


每小时执行/etc/cron.hourly目录内的脚本


0 1 * * *  root run-parts /etc/cron.hourly


每两个小时重启一次


0 */2 * * * /sbin/service httpd restart


每天7:50开启ssh服务 


50 7 * * * /sbin/service sshd start


每天22:50关闭ssh服务


50 22 * * * /sbin/service sshd stop


每月1号和15号检查/duoduo磁盘 


0 0 1,15 * * fsck /duoduo


 每小时的第一分执行 /duoduo这个文件 


1 * * * * /duoduo


每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。


00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \ 


每月的1、11、21、31日是的6:30执行一次ls命令


30 6 */10 * * ls


Linux  Crontab调度_重启_06