在使用Linux的过程中,我们有时需要使用一些定时任务的功能,Linux上常用的计划任务工具有at、batch和cron、anacron,下面我就来简单的说一说。


   at:仅执行一次就从Linux的计划任务中取消

   batch:系统有空就执行

   cron:周期性执行

   anacron:唤醒停机期间的工作任务


(一)、at          

(*依赖于atd服务,需先手动安装 *)

[root@J-7 ~]# yum -y install at

[root@J-7 ~]# systemctl start atd.service 

[root@J-7 ~]# systemctl enable atd.service


语法:at [option] TIME

常用选项:

-V  显示版本信息:

-l:  列出指定队列中等待运行的作业;相当于atq

-d:  删除指定的作业;相当于atrm

-c:  查看具体作业任务

-f /path/from/somefile :从指定的文件中读取任务

-m: 当任务 被完成之后,将给用户发送邮件,即使没有标准输出


TIME:什么时候进行这项任务

常用时间格式:

HH:MM 02:00    在今日的 HH:MM  进行,若该时刻已过,则明天此时执行任务

HH:MM YYYY-MM-DD 02:00 2016-09-20  规定在某年某月的某一天的特殊时刻进行该项任务

HH:MM[am|pm] [Month] [Date]

04pm March 17

17:20 tomorrow

HH:MM[am|pm] + number [minutes|hours|days|weeks]

在某个时间点再加几个时间后才进行该项任务

now + 5 minutes 

02pm + 3 days


范例:

(1)发送一个文件给root

[root@J-7 ~]# at 22:10                     #在22:10点

at> /bin/mail root -s "test job" < /root/.bashrc  #发送.bashrc文件给root

at> <EOT>                               #按Ctrl+d 结束输入

job 2 at Sat Jul  1 22:10:00 201             #第二个任务在22:10进行


(2)两分钟后执行

[root@J-7 ~]# at now + 2 min 

at> /sbin/shutdown -h new

at> /bin/sync

at> <EOT>

job 3 at Sat Jul  1 06:17:00 2017


(3)、查看任务

[root@J-7 ~]# at -l

1Sat Jul  1 22:04:00 2017 a root

2Sat Jul  1 22:10:00 2017 a root

You have mail in /var/spool/mail/root


(4)、删除任务

[root@J-7 ~]# at -d 2

[root@J-7 ~]# at -l

1Sat Jul  1 22:04:00 2017 a root


☆使用权限:

at 队列存放在/var/spool/at 目录中,也可手动进取看任务、删任务

/etc/at.{allow,deny} 控制用户是否能执行at 任务

白名单:/etc/at.allow  默认不存在,只有该文件中的用户才能执行at 命令

[root@J-7 etc]# ls /etc/a

adjtime       alternatives/ at.deny       

aliases       anacrontab    audisp/       

aliases.db    asound.conf   audit/


黑名单:/etc/at.deny  默认存在,拒绝该文件中用户执行at 命令, 而没有在在at.deny文件中的使用者则可执行

[root@J-7 etc]# cat /etc/at.deny 


[root@J-7 etc]# useradd cs

[root@J-7 etc]# echo cs >> /etc/at.deny 

[root@J-7 etc]# cat /etc/at.deny 


cs

[root@J-7 etc]# su - cs

[cs@J-7 ~]$ at now + 2 min

You do not have permission to use at.


如果 

两个文件都存在有 ,则黑名单失效,只有白名单的配置生效 

两个文件都不存在 ,只有 root行可以执行at命令

 


(二)、batch

batch 的原理是利用at来进行命令执行,所以语法和at一样(很多资料说只说了和at一样,然而用的时候后面不用跟时间,因为它是空闲了才执行)。

batch是在系统空闲才执行计划任务,他所谓的空闲即cpu工作负载小于0.8的时候



范例:

关机

[root@J-7 etc]# batch 

at> shutdown

at> <EOT>

job 4 at Sat Jul  1 06:56:00 2017

[root@J-7 etc]#


(三)、cron


语法:

crontab 命令:

crontab [-u user] [-l | -r | -e] [-i]

-l:  列出所有任务;

-e:  编辑任务;

-r:  移除所有任务;

-i :同-r 一同使用,以交互式模式移除指定任务

-u user:  仅root 可运行,指定用户管理cron 任务


任务文件:/var/spool/cron/USERNAME

日志:/var/log/cron


确保crond 守护处于运行状态:

CentOS 7:

systemctl status crond

CentOS 6:

service crond status



系统cron 任务:系统维护作业   /etc/crontab

范例:

[root@J-7 ~]# cat /etc/crontab 

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root


# For details see man 4 crontabs


# 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

 

[root@J-7 ~]#


时间  使用者  执行的命名

(上面星号表示每……一次,默认只有星号,即为没分 每小时 每天 每月 每个星期几)

时间表示法:

(1)  特定值

给定时间点有效取值范围内的值

(2) *

给定时间点上有效取值范围内的所有值

表示“每...”

(3)  离散取值

#,#,#

(4)  连续取值

#-#

(5)  在指定时间范围上,定义步长

/#: #


写关于系统的任务就在这个/etc/crontab里面书写就行


用户cron 任务:crontab命令 使用方法和系统用户的一样,只是存放的地方在/var/spool/cron/目录下  


范例:

10分钟执行一次test.sh脚本

[root@J-7 ~]# crontab -e 

no crontab for root - using an empty one


*/10 * * * * /root/bin/test.sh


每个周六5点30分和女朋友约会

[root@J-7 ~]# crontab -e 


*/10 * * * * /root/bin/test.sh

30 17 * * 5 mail friend@172.168.1.11 < /root/bin/yuehui.txt


查看任务

[root@J-7 ~]# crontab -l

*/10 * * * * /root/bin/test.sh  

30 17 * * 5 mail friend@172.168.1.11 < /root/bin/yuehui.txt  


全部删除(删除个别直接crontab -e 进去删除内容就行)

[root@J-7 ~]# crontab -r

[root@J-7 ~]# crontab -l

no crontab for root


控制用户执行计划任务 :/etc/cron.{allow,deny}

和上述at的黑白名单权限一样,请参照上述说明




(四)、anacron



anacron存在的目的在于处理非24小时不关机的系统crontab任务的执行。所以anacron并不能指定何时执行任务,而是以天为单位或开机后检测停机期间应该进行但没有进行的,执行一遍后自动停止。

任务存放:/var/spool/anacron/*

常用语法:

anacron [-u|-s|-f|-n] [job]

-u:仅仅更新文件的时间戳

-s:开始执行各项工作,根据文件的时间戳判断是否进行

-f:强制进行,不检测

-n;立刻进行,系统不进行其他的任何工作


接下来我们看一下/etc/anacrontab


[root@J-7 ~]# cat /etc/anacrontab 

# /etc/anacrontab: configuration file for anacron


# See anacron(8) and anacrontab(5) for details.


SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# the maximal random delay added to the base delay of the jobs

RANDOM_DELAY=45

# the jobs will be started during the following hours only

START_HOURS_RANGE=3-22


#period in days   delay in minutes   job-identifier   command

1 5 cron.daily nice run-parts /etc/cron.daily

7 25 cron.weekly nice run-parts /etc/cron.weekly

@monthly 45 cron.monthly nice run-parts /etc/cron.monthly


天数    延迟时间  任务名称                实际执行命令 


#天数单位为天,延迟时间为分钟,任务名称自定义,实际执行命令和crontab的写法相同