一次性任务
在某个特定的时间,执行一次后被清除
相关命令/进程
在centos6中,系统服务的名称: /etc/init.d/atd
查看系统上该进程时候启动:
[root@ecs-t6-large-2-linux-20190824103606 ~]# ps -ef | grep atd
root 4472 1 0 Sep04 ? 00:00:00 /usr/sbin/atd -f
root 9628 9222 0 18:21 pts/0 00:00:00 grep --color=auto atd
创建一次性任务
格式:
- at [HH:MM]
- at [HH:MM] [yyyy-mm-dd]
- at now + 数字 [minutes |hours | days | weeks]
例: 创建定时任务,在下一分钟,创建一个文件
[root@ecs-t6-large-2-linux-20190824103606 init.d]# at 18:33
at> touch 123
at> <EOT>
job 3 at Sat Sep 14 18:33:00 2019
[root@ecs-t6-large-2-linux-20190824103606 init.d]# ll
total 44
-rw-r--r-- 1 root root 0 Sep 14 18:33 123
保存定时任务快捷键 ctrl + d
将文件中的命令当作定时任务
格式: at 时间 -f 文件
例: 五分钟后执行 /root/test.sh
at now + 5 minutes -f /root/test.sh
查询当前系统上的一次性定时任务
at -l
[root@ecs-t6-large-2-linux-20190824103606 init.d]# at -l
没任何结果说明没有一次性任务
atq
[root@ecs-t6-large-2-linux-20190824103606 init.d]# atq
没任何结果说明没有一次性任务
[root@ecs-t6-large-2-linux-20190824103606 init.d]# atq
5 Sat Sep 14 20:00:00 2019 a root
5 是任务号
根据任务编号删除指定的一次性任务
atrm [编号]
at -d
查看一次性任务的具体内容
at -c [任务号]
创建的一次性任务文件所在位置
/var/spool/at/a*
在这个路径下,全部a开头的文件都在这里面
[root@ecs-t6-large-2-linux-20190824103606 init.d]# ll /var/spool/at/
total 8
-rwx------ 1 root root 3085 Sep 14 18:37 a00005018ee170
drwx------. 2 root root 4096 Sep 14 18:33 spool
任务执行过之后,这个文件就会消失
在那些文件中显示哪些用户是否可以使用定时任务
-
/etc/at.deny :这个名单中的用户不可以使用定时任务
-
/etc/at.allow: 这个名单中的用户可以使用
周期性任务
按照预订的计划重复执行任务
相关命令/进程
在centos6中,周期任务对应的服务是: /etc/init.d/crond
查看系统上该进程是否被启动
[root@ecs-t6-large-2-linux-20190824103606 ~]# ps -ef | grep crond
root 4474 1 0 Sep04 ? 00:00:00 /usr/sbin/crond -n
root 9699 9222 0 18:23 pts/0 00:00:00 grep --color=auto crond
创建的周期任务文件所在位置
/var/spool/cron/用户名
所以查看系统上是否存在周期任务可以如下:
[root@ecs-t6-large-2-linux-20190824103606 init.d]# cat /var/spool/cron/root
5 11 * * * /tmp/touch_file
cron 服务的配置文件
[root@ecs-t6-large-2-linux-20190824103606 init.d]# 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
注意上面的PATH,这个PATH可能和系统中的PATH不同,这就意味着,当我们命令行敲某些命令时可以根据系统的PATH找到这些命令,但是如果周期任务的PATH中缺少一些路径,就可能导致周期运行命令时失败
[root@ecs-t6-large-2-linux-20190824103606 init.d]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
如果真的出现了不能执行的命令怎么办? 可以在命令前面加上绝对路径 /bin/ 命令
例子: 创建一个脚本文件
[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat touch_file
touch a$RANDOM
执行这个脚本
[root@ecs-t6-large-2-linux-20190824103606 tmp]# bash touch_file
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
total 48
-rw-r--r-- 1 root root 0 Sep 14 18:59 a21698
给这个脚本添加x,让其可执行
[root@ecs-t6-large-2-linux-20190824103606 tmp]# chmod +x touch_file
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
-rwxr-xr-x 1 root root 15 Sep 14 18:59 touch_file
再次执行:
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ./touch_file
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
total 48
-rw-r--r-- 1 root root 0 Sep 14 18:59 a21698
-rw-r--r-- 1 root root 0 Sep 14 19:01 a3460
当我们切换到其他目录时,只能通过添加绝对路径才能运行脚本
[root@ecs-t6-large-2-linux-20190824103606 tmp]# bash /tmp/touch_file
添加到周期任务中
查看cron服务的日志文件
/var/log/cron
从这个日志文件中可以看到历史任务执行记录
管理cron计划任务
- 编辑计划任务:crontab -e [-u 用户名]
- 查看计划任务:crontab -l [-u 用户名]
- 删除计划任务:crontab -r [-u 用户名]
周期任务的格式
[root@ecs-t6-large-2-linux-20190824103606 init.d]# 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,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * *
分钟 小时 日期 月份 星期
0-59 0-23 1-31 1-12 0-7
0 17 * * 1- 5 周一到周五的每天17:00
30 8 * * 1,3,5 星期1,5 每天八点半
0 8-18 * * * 每天的8-18点
0 12 * * 每隔三天的12点
系统级别的计划任务及其扩展anacrontab
适用于下面的两种情况
linux 主机存在定时任务,但主机又不是时时刻刻开机,通过如下配置,可以实现,一开机执行错过的定时任务
我有一个脚本,需要每天都运行一次,但是什么时候运行我们并不关心,但是得运行
[root@ecs-t6-large-2-linux-20190824103606 ~]# 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
# 每天的3点到22点 都可能会启动任务
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
这个延迟的解释是,假如我的八点开机的,八点在 3-22点之间,但是任务不会一开机立即执行,会延迟[5分钟]执行
延迟也可以是 0-45 随机时间
run-parts 命令
run-parts + 路径
执行该目录下的脚本 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|