10.linux 定时任务at与crontab

一、at一次性任务

1.1 命令at安装node

    从文件或标准输入中读取命令并在未来的一个时间执行,只执行一次。at的正常执行须要有守护进程atd.linux

#安装at
    yum install -y at
#启动守护进程
    service atd start 
#查看是否开机启动
    chkconfig --list|grep atd 
#设置开机启动
    chkconfig --level 235 atd on


1.2 使用bash

    若是不使用管道|或指定选项-f的话,at的执行将会是交互式的,须要在at的提示符下输入命令:app

[root@node1 ~]# at now +2 minutes  #执行at并指定执行时刻为如今时间的后两分钟
at> echo hello world > /root/a.txt #手动输入命令并回车
at> <EOT>                #ctrl+d 结束输入
job 2 at 2017-07-24 16:08         #显示任务号及执行时间

选项-l或命令atq查询任务dom

[root@node1 ~]# atq
2       2017-07-24 16:21 a root

[root@node1 ~]# at  -l
2       2017-07-24 16:21 a root

到达时间后任务被执行,生成一个新文件file并保存echo的输出内容ide

[root@node1 ~]# cat a.txt 
hello world


at指定时间的方法:
1)hh:mm小时:分钟(当天,若是时间已过,则在次日执行)
2)midnight(深夜),noon(中午),teatime(下午茶时间,下午4点),today,tomorrow
3)12小时计时制,时间后加am(上午)或pm(下午)
4)指定具体执行日期mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年)
5)相对计时法now + n units,now是如今时刻,n为数字,units是单位(minutes、hours、days、weeks)
url


如明天下午2点20分执行建立一个目录spa

[root@node1]# at 02:20pm tomorrow
at> mkdir /root/temp/x
at> <EOT>
job 11 at Fri Dec 23 14:20:00 2016


选项-d或命令atrm表示删除任务code

[root@node1]# at -d 11 #删除11号任务(上例)
[root@node1]# atq
[root@node1]#


可使用管道|或选项-fat从标准输入或文件中得到任务进程

[root@node1~]# cat test.txt 
echo hello world > /root/temp/file

[root@node1~] at -f test.txt 5pm +2 days
job 12 at Sat Dec 24 17:00:00 2016

[root@node1~]# cat test.txt|at 16:20 12/23/16
job 13 at Fri Dec 23 16:20:00 2016

atd经过两个文件/etc/at.allow/etc/at.deny来决定系统中哪些用户可使用at设置定时任务,它首先检查/etc/at.allow,若是文件存在,则只有文件中列出的用户(每行一个用户名),才能使用at;若是不存在,则检查文件/etc/at.deny,不在此文件中的全部用户均可以使用at。若是/etc/at.deny是空文件,则表示系统中全部用户均可以使用at;若是/etc/at.deny文件也不存在,则只有超级用户(root)才能使用at。


二、crontab

系统中每一个用户均可以拥有本身的cron table,同atd相似,crond也有两个文件/etc/cron.allow/etc/cron.deny用来限制用户使用cron,规则也和atd的两个文件相同。


/etc/cron.deny 表示不能使用crontab 命令的用户

/etc/cron.allow 表示能使用crontab的用户。

若是两个文件同时存在,那么/etc/cron.allow 优先。

若是两个文件都不存在,那么只有超级用户能够安排做业。

每一个用户都会生成一个本身的crontab 文件。这些文件在/var/spool/cron目录下


crontab -u 指定一个用户

crontab -l 列出某个用户的任务计划

crontab -r 删除某个用户的任务

crontab -e 编辑某个用户的任务


对于系统级别的定时任务,这些任务更加剧要,大部分linux系统在/etc中包含了一系列与 cron有关的子目录:/etc/cron.{hourly,daily,weekly,monthly},目录中的文件定义了每小时、天天、每周、每个月须要运行的脚本,运行这些任务的精确时间在文件/etc/crontab中指定。如:

[root@node1 spool]# cat /etc/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

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly




第一部分表示分钟(0-59),* 表示每分钟
第二部分表示小时(0-23),* 表示每小时
第三部分表示日(1-31),  * 表示天天
第四部分表示月(1-12),  * 表示每个月
第五部分表示周几(0-6,0表示周日),* 表示一周中天天
第六部分表示要执行的任务


anacron的目的并非彻底替代cron,是做为cron的一个补充。anacron的任务定义在文件/etc/anacrontab中:

[root@node1 spool]# 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
相关文章
相关标签/搜索