Linux任务计划at
一Linux任务计划介绍
Linux任务计划、周期性任务执行
at:未来的某时间点执行一次任务
batch:系统自行选择空闲时间去执行此处指定的任务
cron:周期性运行某任务
二at计划任务
执行方式:
交互式,输入重定向,at –f 文件
依赖与atd服务,需要启动才能实现at任务,at队列存放在/var/spool/at目录中
(一)安装at包
最小化安装默认是没有安装的,光盘自带的包
[root@centos72 ~]# yum install at -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package at.x86_64 0:3.1.13-23.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================
Package Arch Version Repository Size
========================================================================================================
Installing:
at x86_64 3.1.13-23.el7 base 51 k
Transaction Summary
========================================================================================================
Install 1 Package
Total download size: 51 k
Installed size: 95 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : at-3.1.13-23.el7.x86_64 1/1
Verifying : at-3.1.13-23.el7.x86_64 1/1
Installed:
at.x86_64 0:3.1.13-23.el7
Complete!
查看包的介绍
[root@centos72 ~]# rpm -q at
at-3.1.13-23.el7.x86_64
[root@centos72 ~]# rpm -qi at
Name : at
Version : 3.1.13
Release : 23.el7
Architecture: x86_64
Install Date: Mon 29 Jul 2019 04:42:51 PM CST
Group : System Environment/Daemons
Size : 97561
License : GPLv3+ and GPLv2+ and ISC and MIT and Public Domain
Signature : RSA/SHA256, Wed 25 Apr 2018 06:52:49 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM : at-3.1.13-23.el7.src.rpm
Build Date : Wed 11 Apr 2018 08:13:35 AM CST
Build Host : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://ftp.debian.org/debian/pool/main/a/at
Summary : Job spooling tools
Description :
At and batch read commands from standard input or from a specified
file. At allows you to specify that a command will be run at a
particular time. Batch will execute commands when the system load
levels drop to a particular level. Both commands use user's shell.
You should install the at package if you need a utility for
time-oriented job control. Note: If it is a recurring job that will
need to be repeated at the same time every day/week, etc. you should
use crontab instead.
6版本
[root@centos65 ~]# rpm -q at
at-3.1.10-48.el6.x86_64
[root@centos65 ~]# rpm -qi at
Name : at Relocations: (not relocatable)
Version : 3.1.10 Vendor: CentOS
Release : 48.el6 Build Date: Fri 20 Feb 2015 12:23:33 AM CST
Install Date: Wed 23 Jan 2019 05:30:47 PM CST Build Host: c6b8.bsys.dev.centos.org
Group : System Environment/Daemons Source RPM: at-3.1.10-48.el6.src.rpm
Size : 94221 License: GPLv2+
Signature : RSA/SHA1, Fri 20 Feb 2015 12:34:52 AM CST, Key ID 0946fca2c105b9de
Packager : CentOS BuildSystem <http://bugs.centos.org>
URL : http://ftp.debian.org/debian/pool/main/a/at
Summary : Job spooling tools
Description :
At and batch read commands from standard input or from a specified
file. At allows you to specify that a command will be run at a
particular time. Batch will execute commands when the system load
levels drop to a particular level. Both commands use user's shell.
You should install the at package if you need a utility for
time-oriented job control. Note: If it is a recurring job that will
need to be repeated at the same time every day/week, etc. you should
use crontab instead.
(二)at包的组成
[root@centos72 ~]# rpm -ql at
/etc/at.deny
/etc/pam.d/atd
/etc/sysconfig/atd
/usr/bin/at
/usr/bin/atq
/usr/bin/atrm
/usr/bin/batch
/usr/lib/systemd/system/atd.service
/usr/sbin/atd
/usr/sbin/atrun
/usr/share/doc/at-3.1.13
/usr/share/doc/at-3.1.13/ChangeLog
/usr/share/doc/at-3.1.13/Copyright
/usr/share/doc/at-3.1.13/Problems
/usr/share/doc/at-3.1.13/README
/usr/share/doc/at-3.1.13/timespec
/usr/share/man/man1/at.1.gz
/usr/share/man/man1/atq.1.gz
/usr/share/man/man1/atrm.1.gz
/usr/share/man/man1/batch.1.gz
/usr/share/man/man5/at.allow.5.gz
/usr/share/man/man5/at.deny.5.gz
/usr/share/man/man8/atd.8.gz
/usr/share/man/man8/atrun.8.gz
/var/spool/at
/var/spool/at/.SEQ
/var/spool/at/spool
服务存放位置/etc/rc.d/init.d/atd
运行的程序/usr/sbin/atd
[root@centos65 ~]# rpm -ql at
/etc/at.deny
/etc/pam.d/atd
/etc/rc.d/init.d/atd
/etc/sysconfig/atd
/usr/bin/at
/usr/bin/atq
/usr/bin/atrm
/usr/bin/batch
/usr/lib64/pm-utils/sleep.d/56atd
/usr/sbin/atd
/usr/sbin/atrun
/usr/share/doc/at-3.1.10
/usr/share/doc/at-3.1.10/ChangeLog
/usr/share/doc/at-3.1.10/Copyright
/usr/share/doc/at-3.1.10/Problems
/usr/share/doc/at-3.1.10/README
/usr/share/doc/at-3.1.10/timespec
/usr/share/man/man1/at.1.gz
/usr/share/man/man1/atq.1.gz
/usr/share/man/man1/atrm.1.gz
/usr/share/man/man1/batch.1.gz
/usr/share/man/man5/at.allow.5.gz
/usr/share/man/man8/atd.8.gz
/usr/share/man/man8/atrun.8.gz
/var/spool/at
/var/spool/at/.SEQ
/var/spool/at/spool
查看开机是否自启
因为是比较基础的服务,开机一般自启
[root@centos72 ~]# systemctl is-enabled atd
enabled
[root@centos72 ~]# systemctl is-enabled atd.service
enabled
[root@centos65 ~]# chkconfig --list atd
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@centos65 ~]# runlevel
N 3
服务调用了程序/usr/sbin/atd
[root@centos72 ~]# systemctl status atd.service
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: inactive (dead)
[root@centos72 ~]# systemctl start atd.service
[root@centos72 ~]# systemctl status atd.service
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-05-21 09:28:49 CST; 1s ago
Main PID: 2710 (atd)
CGroup: /system.slice/atd.service
└─2710 /usr/sbin/atd -f
May 21 09:28:49 centos72.huawei.com systemd[1]: Started Job spooling tools.
May 21 09:28:49 centos72.huawei.com systemd[1]: Starting Job spooling tools...
进程编号是2710
[root@centos72 ~]# ps aux | grep 2710
root 2710 0.0 0.0 25904 936 ? Ss 09:28 0:00 /usr/sbin/atd -f
root 2719 0.0 0.0 112704 972 pts/0 S+ 09:30 0:00 grep --color=auto 2710
(三)常用选项
-V 显示版本信息:
-l: 列出指定队列中等待运行的作业;相当于atq
-d: 删除指定的作业;相当于atrm
-c: 查看具体作业任务
-f /path/from/somefile:从指定的文件中读取任务
-m:当任务被完成之后,将给用户发送邮件,即使没有标准输出
注意:作业执行命令的结果中的标准输出和错误以邮件通知给相关用户
(四)at时间格式
HH:MM 02:00
在今日的 HH:MM 进行,若该时刻已过,则明天此时执行任务
HH:MM YYYY-MM-DD 02:00 2018-01-08
规定在某年某月的某一天的特殊时刻进行该项任务
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
三创建at计划任务广播时间
(一)前期准备
1时间同步,虚拟机的时间和宿主机的时间、网络时间要一致
2关闭防火墙
3关闭selinux
(二)创建at计划任务
按ctrl+d保存
[root@centos72 ~]# at 10:00
at> wall 10:00 clock
at> <EOT>
job 1 at Tue May 21 10:00:00 2019
[root@centos72 ~]# at -l
1 Tue May 21 10:00:00 2019 = root
[root@centos72 ~]#
Broadcast message from root@centos72.huawei.com (Tue May 21 10:00:01 2019):
10:00 clock
在创建计划任务完成之后就会写到文件里面了
[root@centos72 ~]# at 10:10
at> wall 10:10 clock
at> <EOT>
job 2 at Tue May 21 10:10:00 2019
[root@centos72 ~]# at -l
2 Tue May 21 10:10:00 2019 a root
[root@centos72 ~]# ls /var/spool/at -l
total 4
-rwx------. 1 root root 2806 May 21 10:04 a00002018c52a2
drwx------. 2 root root 6 May 21 10:00 spool
执行完了任务就会把相应的文件删除了
[root@centos72 ~]# cat /var/spool/at/a00002018c52a2
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=79; export XDG_SESSION_ID
HOSTNAME=centos72.huawei.com; export HOSTNAME
SELINUX_ROLE_REQUESTED=; export SELINUX_ROLE_REQUESTED
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
SSH_CLIENT=192.168.137.1 49197 22; export SSH_CLIENT
SELINUX_USE_CURRENT_RANGE=; export SELINUX_USE_CURRENT_RANGE
SSH_TTY=/dev/pts/0; export SSH_TTY
USER=root; export USER
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=