at 定时任务 crontab
一次性任务
- at
1.1 命令
安装
- at
从文件或标准输入中读取命令并在将来的一个时间执行,只执行一次。
的正常执行需要有守护进程
- at
- atd.
- #安装at yum install - y at#启动守护进程 service atd start #查看是否开机启动 chkconfig --list | grep atd #设置开机启动 chkconfig --level 235 atd on
1.2 使用
如果不使用管道
或指定选项
- |
的话,
- -f
的执行将会是交互式的,需要在 at 的提示符下输入命令:
- at
- [[email protected] ~]# at now + 2 minutes #执行at并指定执行时刻为现在时间的后两分钟at > echo hello world > / root / a.txt #手动输入命令并回车at > #ctrl + d 结束输入job 2 at 2017 - 07 - 24 16 : 08 #显示任务号及执行时间
选项
或命令
- -l
查询任务
- atq
- [[email protected] ~]# atq 2 2017 - 07 - 24 16 : 21 a root
- [[email protected] ~]# at - l 2 2017 - 07 - 24 16 : 21 a root
到达时间后任务被执行,生成一个新文件 file 并保存 echo 的输出内容
- [[email protected] ~]# cat a.txt hello world
指定时间的方法:1)
- at
小时: 分钟 (当天,如果时间已过,则在第二天执行)2)
- hh:mm
(深夜),
- midnight
(中午),
- noon
(下午茶时间,下午 4 点),
- teatime
,
- today
等 3)12 小时计时制,时间后加
- tomorrow
(上午) 或
- am
(下午)4)指定具体执行日期
- pm
(月 / 日 / 年) 或
- mm/dd/yy
(日. 月. 年)5)相对计时法
- dd.mm.yy
,now 是现在时刻,n 为数字,units 是单位 (minutes、hours、days、weeks)
- now + n units
如明天下午 2 点 20 分执行创建一个目录
- [[email protected]]# at 02 : 20pm tomorrow at > mkdir / root / temp / x at > job 11 at Fri Dec 23 14 : 20 : 00 2016
选项
或命令
- -d
表示删除任务
- atrm
- [[email protected]]# at - d 11 #删除11号任务 (上例)[[email protected]]# atq[[email protected]]#
可以使用管道
或选项
- |
让
- -f
从标准输入或文件中获得任务
- at
- [[email protected]~]# cat test.txt echo hello world > / root / temp / file
- [[email protected]~] at - f test.txt 5pm + 2 days job 12 at Sat Dec 24 17 : 00 : 00 2016
- [[email protected]~]# 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
,如果文件存在,则只有文件中列出的用户 (每行一个用户名),才能使用 at;如果不存在,则检查文件
- /etc/at.allow
,不在此文件中的所有用户都可以使用 at。如果
- /etc/at.deny
是空文件,则表示系统中所有用户都可以使用 at;如果
- /etc/at.deny
文件也不存在,则只有超级用户 (root) 才能使用 at。
- /etc/at.deny
- crontab
系统中每个用户都可以拥有自己的
,同
- cron table
类似,
- atd
也有两个文件
- crond
和
- /etc/cron.allow
用来限制用户使用 cron,规则也和
- /etc/cron.deny
的两个文件相同。
- 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
- [[email protected] 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
- [[email protected] 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
来源: http://www.bubuko.com/infodetail-2226718.html