1,at 一次性计划任务

描述:在制定的时间执行特定命令

用法:at 时间

选项:-m 当计划任务执行结束后发送邮件给用户

   -l 查看用户加护任务

   -d 删除用户计划任务

   -c 查看at计划任务的具体内容

例如:

[study@monitor ~]$ at 2:35 #指定时间是当天的2:35开始执行计划任务

at>tar cvf test.tar /study/test.txt

at> cp /study/test.txt /study/test.txt.bak<EOT>  #回车键后可以输入多条命令,ctrl+D结束输入

job 2 at 2016-08-15 02:35


at -l #查看计划任务

at -c 1 #查看编号为1的计划任务的具体内容

at -d 1 #删除编号为1的计划任务

at 命令后面的时间格式有很多,at 小时:分钟(默认代表当天的时间),at 4pm+3days(代表3天后的下午4点执行任务),at 12:00 2016-08-08 指定年月日。具体 man at


2,cron周期性计划任务

使用前首先确定crond服务是否开启,否则计划任务不会被执行

[root@monitor study]# service crond status

crond (pid  1730) is running...

[root@monitor study]# chkconfig --list crond

crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

crontab命令的用法如下:

描述:为每个用户维护周期性的计划任务文件

用法:crontab [-u 指定用户][-l|-r|-e]

选项:-u 指定计划任务的用户,默认为当前用户

-l 查看计划任务

-r 删除计划任务

-e 编辑计划任务

-i 使用-r删除计划任务时,要求用户确认删除

2.1 系统自定义了很多计划任务脚本,分别放在/etc/下的cron.hourly,cron.daily, cron.weekly等,如果有脚本需要每天执行,可以将脚本放在cron.daily目录下,系统会自动每天执行

[root@monitor study]# ls -d /etc/cron*

/etc/cron.d      /etc/cron.deny    /etc/cron.monthly  /etc/cron.weekly

/etc/cron.daily  /etc/cron.hourly  /etc/crontab

时间定义的文件为anacrontab

[root@monitor etc]# cat 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

第一行的意思是:每天开机65分钟后就检查cron.daily文件是否被执行了,如果今天没有被执行就执行他 

第二行的意思是:每隔7天开机后70分钟检查cron.weekly文件是否被执行了,如果一周内没有被执行就执行他

如果要定义这个具体的时间,可在在contab文件中添加以下进行定义,如下

  1. # run-parts  

  2. 01 * * * * root run-parts /etc/cron.hourly  

  3. 02 4 * * * root run-parts /etc/cron.daily  

  4. 22 4 * * 0 root run-parts /etc/cron.weekly  

  5. 42 4 1 * * root run-parts /etc/cron.monthly 

[root@monitor etc]# cat crontab

SHELL=/bin/bash

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

MAILTO=root

HOME=/


# 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

分时日月周 后面是命令


2.2 [root@monitor etc]# crontab -e # 编辑计划任务,会弹出类似于vi的编辑界面

no crontab for root - using an empty one

30 23 * * 5 tar -czf log.tar.bz2 /var/log #每周五晚23点30分执行日志备份

00 */3 * * * who   # 每3小时的整点检查用户登录情况

00 10 * * 3,5 free |mail -s "Memory" admin@xx.com  #每周三,五的10点将系统的内存信息发送到邮箱


2.3 crontab的安全控制:

/etc/cron.allow:
将可以使用 crontab 的帐号写入其中,若不在这个档案内的使用者则不可使用 crontab;

/etc/cron.deny:
将不可以使用 crontab 的帐号写入其中,若未记录到这个档案当中的使用者,就可以使用 crontab。

都是一行一个帐号,/etc/cron.allow 比 /etc/cron.deny 要优先,而判断上面,这两个档案只选择一个来限制而已,因此,建议你只要保留一个即可, 免得影响自己在设定上面的判断!一般来说,系统预设是保留 /etc/cron.deny , 你可以将不想让他执行 crontab 的那个使用者写入 /etc/cron.deny 当中。

当使用者使用 crontab 这个指令来建立工作排程之后,该项工作就会被纪录到 /var/spool/cron/ 里面去了,而且是以帐号来作为判别的。不要使用 vi 直接编辑该档案, 因为可能由于输入语法错误,会导致无法执行 cron

2.4 日志查看:/var/log/cron,里面有多个备份,一周一个,当前的就是cron

2.5 anacron--开机后立刻进行 anacron 的动作,他会去侦测停机期间应该进行但是并没有进行的 crontab 任务,并将该任务执行一遍后,anacron 就会自动停止了,这是由于 anacron 会以一天、七天、一个月为期去侦测系统未进行的 crontab 任务。一般默认设置就可以了。

要确定 anacron 是否开机时会主动的执行,你可以下达下列指令:[root@www ~]# chkconfig --list anacron

2.6 注意事项 服务重启:修改/etc/crontab 之后,可能并不会马上执行, 这个时候请重新启动 crond 这个服务‘/etc/init.d/crond restart’

周与日月不可同时并存,不可使用“几月几号且为星期几”的模式工作