centos

sys.11.2 linux任务计划(at/batch命令举例)

#  at命令操作举例:

[root@kouyuushinn ~]# yum install at             #  如果发现系统中没有'at'命令,可自行安装;


1、创建任务计划:从现在开始3分钟后执行;
---------------------------------------
[root@kouyuushinn ~]# at now+3min
at> ls -l /etc/issue      
at> .
at> <EOT>                                         交互式输入需要执行的任务命令,按组合键'ctrl+d'做为结束符;
job 1 at Wed May  9 06:45:00 2018
Can't open /var/run/atd.pid to signal atd. No atd running?
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# systemctl start atd          上面提示'atd'服务没有运行,可手动启动服务;
[root@kouyuushinn ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      962/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1047/master         
tcp6       0      0 :::22                   :::*                    LISTEN      962/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1047/master         
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# at now+3min                 创建任务计划;
at> ls -l /etc/issue
at> <EOT>
job 2 at Wed May  9 06:47:00 2018
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# at -l                       查看任务计划;
1	Wed May  9 06:45:00 2018 a root
2	Wed May  9 06:47:00 2018 a root
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# at teatime
at> tail /etc/passwd
at> <EOT>
job 3 at Wed May  9 16:00:00 2018
You have new mail in /var/spool/mail/root         任务执行完成后,系统通过邮件服务自动发送任务执行结果到执行者的邮箱中;
[root@kouyuushinn ~]# at -l                       观察:已执行完成的任务计划,不会再次显示;
2	Wed May  9 06:47:00 2018 a root
3	Wed May  9 16:00:00 2018 a root
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# mail                        登录邮箱,查看收到的邮件;
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 3 messages 1 new
    1 Anacron               Tue May  1 20:16  36/1130  "Anacron job 'cron.daily' on localhost.localdomain"
    2 user77@kouyuushinn.c  Wed May  9 05:20  27/801   "Re: dfasfsdfjsdfljdsl"
>N  3 root                  Wed May  9 06:45  16/579   "Output from your job        1"
& 3                                               查看最新的邮件:邮件标记为'3'邮件,就是刚才已经执行完任务计划后的执行结果;
Message  3:                                       
From root@kouyuushinn.cn  Wed May  9 06:45:00 2018
Return-Path: <root@kouyuushinn.cn>
X-Original-To: root
Delivered-To: root@kouyuushinn.cn
Subject: Output from your job        1
To: root@kouyuushinn.cn
Date: Wed,  9 May 2018 06:45:00 +0800 (CST)
From: root@kouyuushinn.cn (root)
Status: R

-rw-r--r--. 1 root root 23 Aug 30  2017 /etc/issue    显示任务的执行结果内容;
/bin/bash: line 2: .: filename argument required
.: usage: . filename [arguments]

& q                                                    退出邮箱;
Held 3 messages in /var/spool/mail/root
[root@kouyuushinn ~]# 


2、指定运行某个以后脚本文件:
----------------------------

[root@kouyuushinn tmp]# at -f /tmp/six.sh now+4min

[root@kouyuushinn tmp]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 6 messages 1 new
    1 Anacron               Tue May  1 20:16  36/1130  "Anacron job 'cron.daily' on localhost.localdomain"
    2 user77@kouyuushinn.c  Wed May  9 05:20  27/801   "Re: dfasfsdfjsdfljdsl"
    3 root                  Wed May  9 06:45  17/590   "Output from your job        1"
    4 root                  Wed May  9 06:47  15/508   "Output from your job        2"
    5 root                  Wed May  9 06:59  16/478   "Output from your job        4"
>N  6 root                  Wed May  9 07:03  33/860   "Output from your job        5"
& 6
Message  6:
From root@kouyuushinn.cn  Wed May  9 07:03:01 2018
Return-Path: <root@kouyuushinn.cn>
X-Original-To: root
Delivered-To: root@kouyuushinn.cn
Subject: Output from your job        5
To: root@kouyuushinn.cn
Date: Wed,  9 May 2018 07:03:01 +0800 (CST)
From: root@kouyuushinn.cn (root)
Status: R


This system has disk device :
Disk /dev/sda
Disk /dev/sdb
Disk /dev/sdc
Disk /dev/sdd
Disk /dev/sde

num_disk is 5.

first_disk_name is /dev/sda.
last_disk_name is /dev/sde.

The last disk device infomation:

Disk /dev/sde: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


& 
At EOF
& q
Held 6 messages in /var/spool/mail/root
[root@kouyuushinn tmp]# 


3、'at'命令创建的任务计划,默认以'a'队列顺序进行,创建任务计划时,可以手动指定队列;
------------------------------------------------------------------------------
[root@kouyuushinn tmp]# 
[root@kouyuushinn tmp]# atq
3	Wed May  9 16:00:00 2018 a root
[root@kouyuushinn tmp]# 
[root@kouyuushinn tmp]# at -q c now+3min
at> cat /etc/issue
at> <EOT>
job 6 at Wed May  9 09:37:00 2018
[root@kouyuushinn tmp]# 
[root@kouyuushinn tmp]# at -l
3	Wed May  9 16:00:00 2018 a root
6	Wed May  9 09:37:00 2018 c root
[root@kouyuushinn tmp]# 


4、删除指定队列序号的任务计划;
-------------------------------
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# atq
3	Wed May  9 16:00:00 2018 a root
7	Wed May  9 09:45:00 2018 f root
8	Wed May  9 09:55:00 2018 e root
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# at -r 3
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# atq
7	Wed May  9 09:45:00 2018 f root
8	Wed May  9 09:55:00 2018 e root
[root@kouyuushinn ~]# 

[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# atq
7	Wed May  9 09:45:00 2018 f root
8	Wed May  9 09:55:00 2018 e root
9	Wed May  9 10:43:00 2018 B root
10	Thu May 10 09:44:00 2018 B root
11	Wed May 16 09:44:00 2018 D root
[root@kouyuushinn ~]# 


5、查看指定队列序号的任务的内容:
-------------------------------
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# at -c 11
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=39; export XDG_SESSION_ID
HOSTNAME=kouyuushinn.cn; export HOSTNAME
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
SSH_CLIENT=192.168.206.1\ 53357\ 22; export SSH_CLIENT
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=01\;35:\*.anx=01\;35:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=01\;36:\*.au=01\;36:\*.flac=01\;36:\*.mid=01\;36:\*.midi=01\;36:\*.mka=01\;36:\*.mp3=01\;36:\*.mpc=01\;36:\*.ogg=01\;36:\*.ra=01\;36:\*.wav=01\;36:\*.axa=01\;36:\*.oga=01\;36:\*.spx=01\;36:\*.xspf=01\;36:; export LS_COLORS
MAIL=/var/spool/mail/root; export MAIL
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH
PWD=/root; export PWD
LANG=en_US.UTF-8; export LANG
HISTCONTROL=ignoredups; export HISTCONTROL
SHLVL=1; export SHLVL
HOME=/root; export HOME
LOGNAME=root; export LOGNAME
SSH_CONNECTION=192.168.206.1\ 53357\ 192.168.206.131\ 22; export SSH_CONNECTION
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
cd /root || {
   echo 'Execution directory inaccessible' >&2
   exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER7331c627'
cat /etc/issue                                此行显示的是作业的具体内容;上面的所有内容是'at'命令的内置的执行任务的环境变量;

marcinDELIMITER7331c627
[root@kouyuushinn ~]# 


2、batch命令举例:
---------------------
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# batch
at> tail /etc/passwd
at> q
at> <EOT>
job 14 at Wed May  9 10:04:00 2018
You have mail in /var/spool/mail/root
[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 12 messages 1 new
    1 Anacron               Tue May  1 20:16  36/1130  "Anacron job 'cron.daily' on localhost.localdomain"
    2 user77@kouyuushinn.c  Wed May  9 05:20  27/801   "Re: dfasfsdfjsdfljdsl"
    3 root                  Wed May  9 06:45  17/590   "Output from your job        1"
    4 root                  Wed May  9 06:47  15/508   "Output from your job        2"
    5 root                  Wed May  9 06:59  16/478   "Output from your job        4"
    6 root                  Wed May  9 07:03  34/871   "Output from your job        5"
    7 root                  Wed May  9 09:37  17/480   "Output from your job        6"
    8 root                  Wed May  9 09:45  17/480   "Output from your job        7"
    9 root                  Wed May  9 09:55  17/480   "Output from your job        8"
   10 root                  Wed May  9 10:02  17/480   "Output from your job       12"
   11 root                  Wed May  9 10:03  38/606   "Output from your job       13"
>N 12 root                  Wed May  9 10:04  24/899   "Output from your job       14"
& 12
Message 12:
From root@kouyuushinn.cn  Wed May  9 10:04:50 2018
Return-Path: <root@kouyuushinn.cn>
X-Original-To: root
Delivered-To: root@kouyuushinn.cn
Subject: Output from your job       14
To: root@kouyuushinn.cn
Date: Wed,  9 May 2018 10:04:50 +0800 (CST)
From: root@kouyuushinn.cn (root)
Status: R

user8:x:3015:3015::/home/user8:/bin/bash
user9:x:3016:3016::/home/user9:/bin/bash
user10:x:3017:3017::/home/user10:/bin/bash
aaa:x:3018:3018::/home/aaa:/bin/bash
user15:x:3019:3019::/home/user15:/bin/bash
user01:x:3020:3020::/home/user01:/bin/bash
user04:x:3021:3021::/home/user04:/bin/bash
user08:x:3022:3022::/home/user08:/bin/bash
user77:x:3023:3023::/home/user77:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin
/bin/bash: line 2: q: command not found

& q
Held 12 messages in /var/spool/mail/root
[root@kouyuushinn ~]# 

 

Leave a Reply

Your email address will not be published. Required fields are marked *