Linux操作系统的计划任务
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.任务计划概述
Linux任务计划、周期性任务执行
未来的某时间点执行一次任务:
at:
指定时间点,执行一次性任务
batch:
系统自行选择空闲时间去执行此处指定的任务
周期性运行某任务:
cron
二.at任务
1>.查看at包
[root@node101.yinzhengjie.org.cn ~]# 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 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rpm -qi at Name : at Version : 3.1.13 Release : 24.el7 Architecture: x86_64 Install Date: Tue 09 Jul 2019 11:07:36 AM CST Group : System Environment/Daemons Size : 97737 License : GPLv3+ and GPLv2+ and ISC and MIT and Public Domain Signature : RSA/SHA256, Mon 12 Nov 2018 10:21:34 PM CST, Key ID 24c6a8a7f4a80eb5 Source RPM : at-3.1.13-24.el7.src.rpm Build Date : Wed 31 Oct 2018 01:10:19 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. [root@node101.yinzhengjie.org.cn ~]#
2>.at任务
at命令:at [option] TIME 常用选项[option]: -V 显示版本信息 -t time 时间格式 [[CC]YY]MMDDhhmm[.ss] -l 列出指定队列中等待运行的作业;相当于atq -d 删除指定的作业;相当于atrm -c 查看具体作业任务 -f /path/file 指定的文件中读取任务 -m 当任务被完成之后,将给用户发送邮件,即使没有标准输出 TIME:定义出什么时候进行at这项任务的时间 HH:MM [YYYY-mm-dd] noon, midnight, teatime(4pm) tomorrow now+#{minutes,hours,days, OR weeks}
遗憾的是at任务本身只能精确到分钟,若想要精确到秒执行,可以使用sleep命令,如让at任务2秒后执行"sleep 2",如果秒也不能满足你的需求,其实还可以使用"usleep"命令来精确到微秒,比如让at任务10微妙后执行"usleep 10"。
执行方式: 1)交互式 2)输入重定向 3)at –f 文件 温馨提示: 作业执行命令的结果中的标准输出和错误以邮件通知给相关用户 依赖于atd服务,需要启动才能实现at任务 at队列存放在/var/spool/at目录中 /etc/at.{allow,deny}控制用户是否能执行at任务 白名单:/etc/at.allow 默认不存在,只有该文件中的用户才能执行at命令,有先级更高,即若黑白名单都存在则以白名单为准。 黑名单:/etc/at.deny 默认存在,拒绝该文件中用户执行at命令,而没有在at.deny 文件中的使用者则可执行 如果两个文件都不存在,只有 root 可以执行 at 命令
[root@node101.yinzhengjie.org.cn ~]# systemctl is-enabled atd #检查atd服务是否开机自启动
enabled
[root@node101.yinzhengjie.org.cn ~]#
3>.at时间格式
HH:MM 02:00 在今日的 HH:MM 进行,若该时刻已过,则明天此时执行任务
HH:MM YYYY-MM-DD 02:00 2016-09-20 规定在某年某月的某一天的特殊时刻进行该项任务
HH:MM[am|pm] [Month] [Date] 04pm March 17 17:20 tomorrow
HH:MM[am|pm] + number [minutes|hours|days|weeks] 在某个时间点再加几个时间后才进行该项任务 now + 5 min 02pm + 3 days
4>.实战案例
[root@node101.yinzhengjie.org.cn ~]# at -l [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# date Mon Nov 18 22:41:26 CST 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at 22:45 #定义一个在22:45执行的一次性任务(操作是关机) at> poweroff at> <EOT> #注意,当指令写完后按"ctrl +d"即可结束at任务编辑 job 1 at Mon Nov 18 22:45:00 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l #查看任务列表 1 Mon Nov 18 22:45:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /var/spool/at/ #一次性任务会自动保存到该目录下 total 4 -rwx------ 1 root root 2897 Nov 18 22:42 a0000101904fb5 drwx------ 2 root root 6 Oct 31 2018 spool [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# at -l [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# date Tue Nov 19 07:22:26 CST 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at 7:25 at> echo https://www.cnblogs.com/yinzhengjie/ at> rm -f /root/a.txt at> <EOT> job 3 at Tue Nov 19 07:25:00 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l 3 Tue Nov 19 07:25:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -c 3 #查看具体作业任务 #!/bin/sh # atrun uid=0 gid=0 # mail root 0 umask 22 XDG_SESSION_ID=1; export XDG_SESSION_ID HOSTNAME=node101.yinzhengjie.org.cn; export HOSTNAME SHELL=/bin/bash; export SHELL HISTSIZE=1000; export HISTSIZE SSH_CLIENT=172.30.1.254 59632 22; export SSH_CLIENT QTDIR=/usr/lib64/qt-3.3; export QTDIR QTINC=/usr/lib64/qt-3.3/include; export QTINC SSH_TTY=/dev/pts/0; export SSH_TTY QT_GRAPHICSSYSTEM_CHECKED=1; export QT_GRAPHICSSYSTEM_CHECKED 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=3 7;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_COLORSMAIL=/var/spool/mail/root; export MAIL PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/softwares/jdk1.8.0_201/bin:/root/bin:/home/softwares /mysql/bin/; export PATHPWD=/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 QTLIB=/usr/lib64/qt-3.3/lib; export QTLIB SSH_CONNECTION=172.30.1.254 59632 172.30.1.101 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} << 'marcinDELIMITER1feb7a08' echo https://www.cnblogs.com/yinzhengjie/ rm -f /root/a.txt marcinDELIMITER1feb7a08 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# at -l 5 Tue Nov 19 07:39:00 2019 a root 6 Tue Nov 19 08:49:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -d 5 #删除计划任务 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l 6 Tue Nov 19 08:49:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# at -l 6 Tue Nov 19 08:49:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo -e "poweroff touch /root/a.txt" | at 12:10 #输入重定向的方式定义at任务 job 7 at Tue Nov 19 12:10:00 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l 6 Tue Nov 19 08:49:00 2019 a root 7 Tue Nov 19 12:10:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -c 7 #!/bin/sh # atrun uid=0 gid=0 # mail root 0 umask 22 XDG_SESSION_ID=1; export XDG_SESSION_ID HOSTNAME=node101.yinzhengjie.org.cn; export HOSTNAME SHELL=/bin/bash; export SHELL HISTSIZE=1000; export HISTSIZE SSH_CLIENT=172.30.1.254 59632 22; export SSH_CLIENT QTDIR=/usr/lib64/qt-3.3; export QTDIR QTINC=/usr/lib64/qt-3.3/include; export QTINC SSH_TTY=/dev/pts/0; export SSH_TTY QT_GRAPHICSSYSTEM_CHECKED=1; export QT_GRAPHICSSYSTEM_CHECKED 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=3 7;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_COLORSMAIL=/var/spool/mail/root; export MAIL PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/softwares/jdk1.8.0_201/bin:/root/bin:/home/softwares /mysql/bin/; export PATHPWD=/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 QTLIB=/usr/lib64/qt-3.3/lib; export QTLIB SSH_CONNECTION=172.30.1.254 59632 172.30.1.101 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} << 'marcinDELIMITER174f2c3d' poweroff touch /root/a.txt marcinDELIMITER174f2c3d [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# date #观察当前时间 Tue Nov 19 07:34:10 CST 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l 6 Tue Nov 19 08:49:00 2019 a root 7 Tue Nov 19 12:10:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at now+10minutes #让任务10分钟之后执行 at> echo 10 min at> <EOT> job 8 at Tue Nov 19 07:44:00 2019 #我们发现的确是任务添加到10分钟之后了。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l 6 Tue Nov 19 08:49:00 2019 a root 7 Tue Nov 19 12:10:00 2019 a root 8 Tue Nov 19 07:44:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -c 8 #!/bin/sh # atrun uid=0 gid=0 # mail root 0 umask 22 XDG_SESSION_ID=1; export XDG_SESSION_ID HOSTNAME=node101.yinzhengjie.org.cn; export HOSTNAME SHELL=/bin/bash; export SHELL HISTSIZE=1000; export HISTSIZE SSH_CLIENT=172.30.1.254 59632 22; export SSH_CLIENT QTDIR=/usr/lib64/qt-3.3; export QTDIR QTINC=/usr/lib64/qt-3.3/include; export QTINC SSH_TTY=/dev/pts/0; export SSH_TTY QT_GRAPHICSSYSTEM_CHECKED=1; export QT_GRAPHICSSYSTEM_CHECKED 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=3 7;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_COLORSMAIL=/var/spool/mail/root; export MAIL PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/softwares/jdk1.8.0_201/bin:/root/bin:/home/softwares /mysql/bin/; export PATHPWD=/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 QTLIB=/usr/lib64/qt-3.3/lib; export QTLIB SSH_CONNECTION=172.30.1.254 59632 172.30.1.101 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} << 'marcinDELIMITER58f2aecc' echo 10 min marcinDELIMITER58f2aecc [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# at 18:10 <<EOF #也可以使用类似于脚本的方式定义at任务 > touch /root/b.txt > touch /root/c.txt > echo "https://www.cnblogs.com/yinzhengjie/" > EOF job 9 at Tue Nov 19 18:10:00 2019 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l 6 Tue Nov 19 08:49:00 2019 a root 7 Tue Nov 19 12:10:00 2019 a root 9 Tue Nov 19 18:10:00 2019 a root [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# su - yinzhengjie #我们使用"yinzhengjie"用户定义一个at任务 Last login: Mon Nov 18 19:40:48 CST 2019 on pts/0 [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ at 18:30 at> poweroff at> cat /etc/shadow at> <EOT> job 10 at Tue Nov 19 18:30:00 2019 [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ at -l #查看咱们定义好的任务 10 Tue Nov 19 18:30:00 2019 a yinzhengjie [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ exit logout [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# vim /etc/at.deny #编辑黑名单,让指定用户无权限和查看定义at任务 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/at.deny yinzhengjie [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# su - yinzhengjie Last login: Tue Nov 19 07:46:51 CST 2019 on pts/0 [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ at -l You do not have permission to use at. [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ at 19:00 You do not have permission to use at. [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ exit logout [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# at -l #虽说"yinzhengjie"已经无权限定义at任务了,但该用户之前定义的任务依旧会执行。 6 Tue Nov 19 08:49:00 2019 a root 7 Tue Nov 19 12:10:00 2019 a root 9 Tue Nov 19 18:10:00 2019 a root 10 Tue Nov 19 18:30:00 2019 a yinzhengjie [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
三.周期性任务计划cron(注意,进入单用户模式下计划任务是不会执行的)
1>.相关的程序包
[root@node101.yinzhengjie.org.cn ~]# rpm -ql cronie #主程序包,提供crond守护进程及相关辅助工具 /etc/cron.d /etc/cron.d/0hourly /etc/cron.deny /etc/pam.d/crond /etc/sysconfig/crond /usr/bin/crontab /usr/lib/systemd/system/crond.service /usr/sbin/crond /usr/share/doc/cronie-1.4.11 /usr/share/doc/cronie-1.4.11/AUTHORS /usr/share/doc/cronie-1.4.11/COPYING /usr/share/doc/cronie-1.4.11/ChangeLog /usr/share/doc/cronie-1.4.11/INSTALL /usr/share/doc/cronie-1.4.11/README /usr/share/man/man1/crontab.1.gz /usr/share/man/man5/crontab.5.gz /usr/share/man/man8/cron.8.gz /usr/share/man/man8/crond.8.gz /var/spool/cron [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rpm -ql cronie-anacron #cronie的补充程序,用于监控cronie任务执行状况,如cronie中的任务在过去该运行的时间点未能正常运行,则anacron会随后启动一次此任务 /etc/anacrontab /etc/cron.hourly/0anacron /usr/sbin/anacron /usr/share/man/man5/anacrontab.5.gz /usr/share/man/man8/anacron.8.gz /var/spool/anacron /var/spool/anacron/cron.daily /var/spool/anacron/cron.monthly /var/spool/anacron/cron.weekly [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rpm -ql crontabs #包含CentOS提供系统维护任务 /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly /etc/crontab /etc/sysconfig/run-parts /usr/bin/run-parts /usr/share/man/man4/crontabs.4.gz /usr/share/man/man4/run-parts.4.gz [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
2>.确保crond守护处于运行状态
[root@node101.yinzhengjie.org.cn ~]# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-11-20 05:41:02 CST; 5min ago Main PID: 3060 (crond) CGroup: /system.slice/crond.service └─3060 /usr/sbin/crond -n Nov 20 05:41:02 node101.yinzhengjie.org.cn systemd[1]: Started Command Scheduler. Nov 20 05:41:02 node101.yinzhengjie.org.cn crond[3060]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 72% if used.) Nov 20 05:41:02 node101.yinzhengjie.org.cn crond[3060]: (CRON) INFO (running with inotify support) [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# service crond status Redirecting to /bin/systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-11-20 05:41:02 CST; 6min ago Main PID: 3060 (crond) CGroup: /system.slice/crond.service └─3060 /usr/sbin/crond -n Nov 20 05:41:02 node101.yinzhengjie.org.cn systemd[1]: Started Command Scheduler. Nov 20 05:41:02 node101.yinzhengjie.org.cn crond[3060]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 72% if used.) Nov 20 05:41:02 node101.yinzhengjie.org.cn crond[3060]: (CRON) INFO (running with inotify support) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
3>.run-parts工具
[root@node101.yinzhengjie.org.cn ~]# mkdir shell [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "https://www.cnblogs.com/yinzhengjie/" > shell/blog.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "ansible,jekins,gitlab,gerrit,python,golang" > shell/devops.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# chmod +x -R shell/ [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll shell/ total 8 -rwxr-xr-x 1 root root 37 Nov 20 05:50 blog.txt -rwxr-xr-x 1 root root 43 Nov 20 05:52 devops.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# run-parts shell/ #可以将一个目录下所有可执行脚本统统跑一遍 shell/blog.txt: shell/blog.txt: line 1: https://www.cnblogs.com/yinzhengjie/: No such file or directory shell/devops.txt: shell/devops.txt: line 1: ansible,jekins,gitlab,gerrit,python,golang: command not found [root@node101.yinzhengjie.org.cn ~]#
4>.时间表示法
特定值: 给定时间点有效取值范围内的值,如表示星期的取值范围为"0-7"(0或者7都表示周日)。月份的取值范围则为"1-12"等。 *: 给定时间点上有效取值范围内的所有值,比如想要表示1-12个月份,那么就不用这么啰嗦,直接使用"*"号(表示"每...")代替即可。 #,#,#: 连续取值,比如每周1,3,5九点开始上课,则表示为"00 09 1,3,5 * *"。 #-# 在指定时间范围上,定义步长,比如像表示周一到周五,则"* * * * 1-5"。 /#: "#"即为步长,比如每十分钟则"*/10 * * * *"。 时间格式: @reboot:
表示下次重启才会执行。 @yearly 0 0 1 1 * @annually 0 0 1 1 * @monthly 0 0 1 * * @weekly 0 0 * * 0 @daily 0 0 * * * @hourly 0 * * * * 示例:每3小时echo和wall命令 0 */3 * * * centos /bin/echo "Hello!"; wall "welcome to my blog:'https://www.cnblogs.com/yinzhengjie/'"
5>.计划周期性执行的任务提交给crond,到指定时间会自动运行
系统cron任务:系统维护作业 /etc/crontab(仅适合root用户编写它) 用户cron任务: crontab命令,适合所有用户去编辑自己的计划任务它是拥有SUID权限的命令。 crontab命令定义: 每个用户都有专用的cron任务文件:/var/spool/cron/USERNAME crontab语法格式: crontab [-u user] [-l | -r | -e] [-i] -l: 列出所有任务 -e: 编辑任务 -r: 移除所有任务 -i: 同-r一同使用,以交互式模式移除指定任务 -u: user 仅root可运行,指定用户管理cron任务 控制用户执行计划任务: /etc/cron.{allow,deny} 温馨提示: 更多帮助信息可参考:"man 5 crontab" 运行结果的标准输出和错误以邮件通知给相关用户 COMMAND > /dev/null COMMAND &> /dev/null 对于cron任务来讲,"%"有特殊用途;如果在命令中要使用"%",则需要转义,将"%"放置于单引号中,则可不用转义
[root@node101.yinzhengjie.org.cn ~]# cat /etc/crontab #root用户定义系统cron任务,该文件的属主属组都是root用户。更多帮助信息详情参见"man 5 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 #这里提示我们0或者7表示星期日,0-6表示周日,周一,周二,......,周六。 # | | | | | # * * * * * user-name command to be executed * 3-6,12 0 root /bin/tar cf /root/shell.tar /root/shell &> /dev/null #每年的3,4,5,6,12月份的周日的2:30分都会以root身份执行一下"/bin/tar cf /root/shell.tar /root/shell &> /dev/null"命令,注意命令必须写绝对路径哟~ */10 * * * * root /root/shell/checkdisk.sh #表示每间隔10分钟就以root身份执行一下"checkdisk.sh"脚本 1,15 * 7 root /root/shell/f1.sh #每个月的1,15号或者每个星期日的4:30分都会以root身份执行一下"/root/shell/f1.sh"脚本 @reboot root /bin/rm -rf /data/* #下次重启才会执行该操作,注意,如果重启时进入单用户模式下计划任务是不会执行的。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /etc/crontab #仅仅适合root用户,因为它的权限为root用户 -rw-r--r-- 1 root root 647 Nov 20 06:37 /etc/crontab [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# which crontab /usr/bin/crontab [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /usr/bin/crontab #注意,该命令是拥有SUID权限的哟 -rwsr-xr-x. 1 root root 57576 Apr 11 2018 /usr/bin/crontab [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# su -l yinzhengjie Last login: Tue Nov 19 07:48:42 CST 2019 on pts/0 [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ crontab -e #编辑任务列表,会自动生成一个中间临时文件,编辑并保存成功后会自动写入专用的cron任务文件,即"/var/spool/cron/yinzhengjie" no crontab for yinzhengjie - using an empty one crontab: installing new crontab [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ crontab -l #查看已经定义的计划任务列表 * * * * * /bin/wall Wellcome to my blog[https://www.cnblogs.com/yinzhengjie/]. [yinzhengjie@node101.yinzhengjie.org.cn ~]$ [yinzhengjie@node101.yinzhengjie.org.cn ~]$ exit logout [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# crontab -l -u yinzhengjie #查看指定用户的计划任务列表 * * * * * /bin/wall Wellcome to my blog[https://www.cnblogs.com/yinzhengjie/]. [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# crontab -l #查看当前root用的计划任务列表,发现当前任务列表为空。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /var/spool/cron/ #每个用户都有专用的cron任务文件 total 4 -rw------- 1 root root 0 Nov 20 07:01 root -rw------- 1 yinzhengjie yinzhengjie 79 Nov 20 07:03 yinzhengjie [root@node101.yinzhengjie.org.cn ~]#
6>.日志
[root@node101.yinzhengjie.org.cn ~]# cat /var/log/cron Jul 8 17:03:42 localhost crond[3092]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 56% if used.) Jul 8 17:03:42 localhost crond[3092]: (CRON) INFO (running with inotify support) Jul 8 17:14:58 node101 crond[3044]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 85% if used.) Jul 8 17:14:58 node101 crond[3044]: (CRON) INFO (running with inotify support) Jul 8 18:01:01 node101 CROND[6026]: (root) CMD (run-parts /etc/cron.hourly) Jul 8 18:01:01 node101 run-parts(/etc/cron.hourly)[6026]: starting 0anacron Jul 8 18:01:01 node101 anacron[6035]: Anacron started on 2019-07-08 Jul 8 18:01:01 node101 run-parts(/etc/cron.hourly)[6037]: finished 0anacron Jul 8 18:01:01 node101 anacron[6035]: Will run job `cron.daily' in 48 min. Jul 8 18:01:01 node101 anacron[6035]: Will run job `cron.weekly' in 68 min. Jul 8 18:01:01 node101 anacron[6035]: Will run job `cron.monthly' in 88 min. Jul 8 18:01:01 node101 anacron[6035]: Jobs will be executed sequentially Jul 8 18:08:41 node101 crond[3069]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 43% if used.) Jul 8 18:08:41 node101 crond[3069]: (CRON) INFO (running with inotify support) Jul 8 18:35:25 node101 crond[3069]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 37% if used.) Jul 8 18:35:25 node101 crond[3069]: (CRON) INFO (running with inotify support) Jul 9 10:13:17 node101 crond[3069]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 77% if used.) Jul 9 10:13:17 node101 crond[3069]: (CRON) INFO (running with inotify support) Jul 9 10:49:15 node101 crond[3065]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 28% if used.) Jul 9 10:49:15 node101 crond[3065]: (CRON) INFO (running with inotify support) Jul 9 11:01:01 node101 CROND[13834]: (root) CMD (run-parts /etc/cron.hourly) Jul 9 11:01:01 node101 run-parts(/etc/cron.hourly)[13834]: starting 0anacron Jul 9 11:01:01 node101 anacron[13843]: Anacron started on 2019-07-09 Jul 9 11:01:01 node101 anacron[13843]: Will run job `cron.daily' in 35 min. Jul 9 11:01:01 node101 anacron[13843]: Will run job `cron.weekly' in 55 min. Jul 9 11:01:01 node101 anacron[13843]: Will run job `cron.monthly' in 75 min. Jul 9 11:01:01 node101 anacron[13843]: Jobs will be executed sequentially Jul 9 11:01:01 node101 run-parts(/etc/cron.hourly)[13845]: finished 0anacron Jul 9 11:07:24 node101 crond[3065]: (CRON) INFO (Shutting down) Jul 9 11:07:24 node101 crond[13919]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 82% if used.) Jul 9 11:07:24 node101 crond[13919]: (CRON) INFO (running with inotify support) Jul 9 11:07:24 node101 crond[13919]: (CRON) INFO (@reboot jobs will be run at computer's startup.) Nov 17 21:32:59 node101 crond[3051]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 3% if used.) Nov 17 21:32:59 node101 crond[3051]: (CRON) INFO (running with inotify support) Nov 17 22:01:01 node101 CROND[3798]: (root) CMD (run-parts /etc/cron.hourly) Nov 17 22:01:01 node101 run-parts(/etc/cron.hourly)[3798]: starting 0anacron Nov 17 22:01:01 node101 anacron[3807]: Anacron started on 2019-11-17 Nov 17 22:01:01 node101 anacron[3807]: Will run job `cron.daily' in 25 min. Nov 17 22:01:01 node101 anacron[3807]: Will run job `cron.weekly' in 45 min. Nov 17 22:01:01 node101 anacron[3807]: Will run job `cron.monthly' in 65 min. Nov 17 22:01:01 node101 anacron[3807]: Jobs will be executed sequentially Nov 17 22:01:01 node101 run-parts(/etc/cron.hourly)[3809]: finished 0anacron Nov 17 22:26:01 node101 anacron[3807]: Job `cron.daily' started Nov 17 22:26:01 node101 run-parts(/etc/cron.daily)[3815]: starting logrotate Nov 17 22:26:01 node101 run-parts(/etc/cron.daily)[3823]: finished logrotate Nov 17 22:26:01 node101 run-parts(/etc/cron.daily)[3815]: starting man-db.cron Nov 17 22:26:06 node101 run-parts(/etc/cron.daily)[15184]: finished man-db.cron Nov 17 22:26:06 node101 anacron[3807]: Job `cron.daily' terminated Nov 17 22:46:01 node101 anacron[3807]: Job `cron.weekly' started Nov 17 22:46:01 node101 anacron[3807]: Job `cron.weekly' terminated Nov 17 23:01:01 node101 CROND[15193]: (root) CMD (run-parts /etc/cron.hourly) Nov 17 23:01:01 node101 run-parts(/etc/cron.hourly)[15193]: starting 0anacron Nov 17 23:01:01 node101 run-parts(/etc/cron.hourly)[15202]: finished 0anacron Nov 17 23:06:01 node101 anacron[3807]: Job `cron.monthly' started Nov 17 23:06:01 node101 anacron[3807]: Job `cron.monthly' terminated Nov 17 23:06:01 node101 anacron[3807]: Normal exit (3 jobs run) Nov 18 00:01:01 node101 CROND[15214]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 00:01:01 node101 run-parts(/etc/cron.hourly)[15214]: starting 0anacron Nov 18 00:01:01 node101 anacron[15223]: Anacron started on 2019-11-18 Nov 18 00:01:01 node101 anacron[15223]: Normal exit (0 jobs run) Nov 18 00:01:01 node101 run-parts(/etc/cron.hourly)[15225]: finished 0anacron Nov 18 01:01:01 node101 CROND[15239]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 01:01:01 node101 run-parts(/etc/cron.hourly)[15239]: starting 0anacron Nov 18 01:01:01 node101 anacron[15248]: Anacron started on 2019-11-18 Nov 18 01:01:01 node101 anacron[15248]: Normal exit (0 jobs run) Nov 18 01:01:01 node101 run-parts(/etc/cron.hourly)[15250]: finished 0anacron Nov 18 02:01:01 node101 CROND[15259]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 02:01:01 node101 run-parts(/etc/cron.hourly)[15259]: starting 0anacron Nov 18 02:01:01 node101 anacron[15268]: Anacron started on 2019-11-18 Nov 18 02:01:01 node101 anacron[15268]: Normal exit (0 jobs run) Nov 18 02:01:01 node101 run-parts(/etc/cron.hourly)[15270]: finished 0anacron Nov 18 03:01:01 node101 CROND[15278]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 03:01:01 node101 run-parts(/etc/cron.hourly)[15278]: starting 0anacron Nov 18 03:01:01 node101 anacron[15287]: Anacron started on 2019-11-18 Nov 18 03:01:01 node101 anacron[15287]: Will run job `cron.daily' in 48 min. Nov 18 03:01:01 node101 anacron[15287]: Jobs will be executed sequentially Nov 18 03:01:01 node101 run-parts(/etc/cron.hourly)[15289]: finished 0anacron Nov 18 03:49:01 node101 anacron[15287]: Job `cron.daily' started Nov 18 03:49:01 node101 run-parts(/etc/cron.daily)[15298]: starting logrotate Nov 18 03:49:01 node101 run-parts(/etc/cron.daily)[15306]: finished logrotate Nov 18 03:49:01 node101 run-parts(/etc/cron.daily)[15298]: starting man-db.cron Nov 18 03:49:01 node101 run-parts(/etc/cron.daily)[15317]: finished man-db.cron Nov 18 03:49:01 node101 anacron[15287]: Job `cron.daily' terminated Nov 18 03:49:01 node101 anacron[15287]: Normal exit (1 job run) Nov 18 04:01:01 node101 CROND[15324]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 04:01:01 node101 run-parts(/etc/cron.hourly)[15324]: starting 0anacron Nov 18 04:01:01 node101 run-parts(/etc/cron.hourly)[15333]: finished 0anacron Nov 18 05:01:01 node101 CROND[15343]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 05:01:01 node101 run-parts(/etc/cron.hourly)[15343]: starting 0anacron Nov 18 05:01:01 node101 run-parts(/etc/cron.hourly)[15352]: finished 0anacron Nov 18 06:01:01 node101 CROND[15490]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 06:01:01 node101 run-parts(/etc/cron.hourly)[15490]: starting 0anacron Nov 18 06:01:01 node101 run-parts(/etc/cron.hourly)[15499]: finished 0anacron Nov 18 06:24:45 node101 crond[3075]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 96% if used.) Nov 18 06:24:45 node101 crond[3075]: (CRON) INFO (running with inotify support) Nov 18 06:25:52 node101 crond[3075]: (CRON) INFO (Shutting down) Nov 18 06:26:09 node101 crond[3059]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 11% if used.) Nov 18 06:26:09 node101 crond[3059]: (CRON) INFO (running with inotify support) Nov 18 06:28:34 node101 crond[3067]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 17% if used.) Nov 18 06:28:34 node101 crond[3067]: (CRON) INFO (running with inotify support) Nov 18 07:01:01 node101 CROND[4281]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 07:01:01 node101 run-parts(/etc/cron.hourly)[4281]: starting 0anacron Nov 18 07:01:01 node101 run-parts(/etc/cron.hourly)[4290]: finished 0anacron Nov 18 08:01:01 node101 CROND[7123]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 08:01:01 node101 run-parts(/etc/cron.hourly)[7123]: starting 0anacron Nov 18 08:01:01 node101 run-parts(/etc/cron.hourly)[7132]: finished 0anacron Nov 18 09:01:01 node101 CROND[7227]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 09:01:01 node101 run-parts(/etc/cron.hourly)[7227]: starting 0anacron Nov 18 09:01:01 node101 run-parts(/etc/cron.hourly)[7236]: finished 0anacron Nov 18 10:01:01 node101 CROND[7257]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 10:01:01 node101 run-parts(/etc/cron.hourly)[7257]: starting 0anacron Nov 18 10:01:01 node101 run-parts(/etc/cron.hourly)[7266]: finished 0anacron Nov 18 11:01:01 node101 CROND[7271]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 11:01:01 node101 run-parts(/etc/cron.hourly)[7271]: starting 0anacron Nov 18 11:01:01 node101 run-parts(/etc/cron.hourly)[7280]: finished 0anacron Nov 18 12:01:01 node101 CROND[7295]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 12:01:01 node101 run-parts(/etc/cron.hourly)[7295]: starting 0anacron Nov 18 12:01:01 node101 run-parts(/etc/cron.hourly)[7304]: finished 0anacron Nov 18 13:01:01 node101 CROND[7561]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 13:01:01 node101 run-parts(/etc/cron.hourly)[7561]: starting 0anacron Nov 18 13:01:01 node101 run-parts(/etc/cron.hourly)[7570]: finished 0anacron Nov 18 14:01:01 node101 CROND[7958]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 14:01:01 node101 run-parts(/etc/cron.hourly)[7958]: starting 0anacron Nov 18 14:01:01 node101 run-parts(/etc/cron.hourly)[7967]: finished 0anacron Nov 18 15:01:01 node101 CROND[8230]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 15:01:01 node101 run-parts(/etc/cron.hourly)[8230]: starting 0anacron Nov 18 15:01:01 node101 run-parts(/etc/cron.hourly)[8239]: finished 0anacron Nov 18 16:01:01 node101 CROND[8492]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 16:01:01 node101 run-parts(/etc/cron.hourly)[8492]: starting 0anacron Nov 18 16:01:01 node101 run-parts(/etc/cron.hourly)[8501]: finished 0anacron Nov 18 16:05:08 node101 crond[3060]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 30% if used.) Nov 18 16:05:08 node101 crond[3060]: (CRON) INFO (running with inotify support) Nov 18 17:01:01 node101 CROND[3990]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 17:01:01 node101 run-parts(/etc/cron.hourly)[3990]: starting 0anacron Nov 18 17:01:01 node101 run-parts(/etc/cron.hourly)[3999]: finished 0anacron Nov 18 17:40:01 node101 CROND[4342]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 17:50:01 node101 CROND[4388]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 18:00:01 node101 CROND[4567]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 18:01:01 node101 CROND[4592]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 18:01:01 node101 run-parts(/etc/cron.hourly)[4592]: starting 0anacron Nov 18 18:01:01 node101 run-parts(/etc/cron.hourly)[4601]: finished 0anacron Nov 18 18:10:01 node101 CROND[4776]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 18:20:01 node101 CROND[4862]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 18:30:01 node101 CROND[4900]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 18:40:01 node101 CROND[4924]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 18:50:01 node101 CROND[5200]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 19:00:01 node101 CROND[5281]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 19:01:01 node101 CROND[5302]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 19:01:01 node101 run-parts(/etc/cron.hourly)[5302]: starting 0anacron Nov 18 19:01:01 node101 run-parts(/etc/cron.hourly)[5311]: finished 0anacron Nov 18 19:10:01 node101 CROND[5344]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 19:20:01 node101 CROND[5401]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 19:23:48 node101 crond[3060]: (CRON) INFO (Shutting down) Nov 18 19:24:06 node101 crond[3062]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 88% if used.) Nov 18 19:24:06 node101 crond[3062]: (CRON) INFO (running with inotify support) Nov 18 19:30:01 node101 CROND[3946]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 19:40:01 node101 CROND[4126]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 19:50:01 node101 CROND[4193]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 20:00:01 node101 CROND[4208]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 20:01:01 node101 CROND[4214]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 20:01:01 node101 run-parts(/etc/cron.hourly)[4214]: starting 0anacron Nov 18 20:01:01 node101 run-parts(/etc/cron.hourly)[4223]: finished 0anacron Nov 18 20:10:01 node101 CROND[4305]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 20:20:01 node101 CROND[4317]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 20:30:01 node101 CROND[4324]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 20:40:01 node101 CROND[4332]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 20:50:01 node101 CROND[4339]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 21:00:01 node101 CROND[4347]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 21:01:01 node101 CROND[4353]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 21:01:01 node101 run-parts(/etc/cron.hourly)[4353]: starting 0anacron Nov 18 21:01:01 node101 run-parts(/etc/cron.hourly)[4362]: finished 0anacron Nov 18 21:10:01 node101 CROND[4366]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 21:20:01 node101 CROND[4399]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 21:30:01 node101 CROND[4405]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 21:40:01 node101 CROND[4410]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 21:50:01 node101 CROND[4418]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 22:00:01 node101 CROND[4425]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 22:01:01 node101 CROND[4430]: (root) CMD (run-parts /etc/cron.hourly) Nov 18 22:01:01 node101 run-parts(/etc/cron.hourly)[4430]: starting 0anacron Nov 18 22:01:01 node101 run-parts(/etc/cron.hourly)[4439]: finished 0anacron Nov 18 22:10:01 node101 CROND[4444]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 22:20:01 node101 CROND[4450]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 22:30:01 node101 CROND[4457]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 18 22:40:01 node101 CROND[4468]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 07:17:35 node101 crond[3068]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 87% if used.) Nov 19 07:17:35 node101 crond[3068]: (CRON) INFO (running with inotify support) Nov 19 07:20:01 node101 CROND[3850]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 07:30:02 node101 CROND[3956]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 07:40:01 node101 CROND[3979]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 07:50:01 node101 CROND[4083]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 08:00:01 node101 CROND[4099]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 08:01:01 node101 CROND[4105]: (root) CMD (run-parts /etc/cron.hourly) Nov 19 08:01:01 node101 run-parts(/etc/cron.hourly)[4105]: starting 0anacron Nov 19 08:01:01 node101 anacron[4114]: Anacron started on 2019-11-19 Nov 19 08:01:01 node101 run-parts(/etc/cron.hourly)[4116]: finished 0anacron Nov 19 08:01:01 node101 anacron[4114]: Will run job `cron.daily' in 16 min. Nov 19 08:01:01 node101 anacron[4114]: Jobs will be executed sequentially Nov 19 08:10:01 node101 CROND[4121]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 08:17:01 node101 anacron[4114]: Job `cron.daily' started Nov 19 08:17:01 node101 run-parts(/etc/cron.daily)[4127]: starting logrotate Nov 19 08:17:01 node101 run-parts(/etc/cron.daily)[4135]: finished logrotate Nov 19 08:17:01 node101 run-parts(/etc/cron.daily)[4127]: starting man-db.cron Nov 19 08:17:01 node101 run-parts(/etc/cron.daily)[4678]: finished man-db.cron Nov 19 08:17:01 node101 anacron[4114]: Job `cron.daily' terminated Nov 19 08:17:01 node101 anacron[4114]: Normal exit (1 job run) Nov 19 08:20:01 node101 CROND[4682]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 08:30:01 node101 CROND[4688]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 08:40:01 node101 CROND[4695]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 08:50:01 node101 CROND[4707]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 09:00:01 node101 CROND[4714]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 09:01:01 node101 CROND[4719]: (root) CMD (run-parts /etc/cron.hourly) Nov 19 09:01:01 node101 run-parts(/etc/cron.hourly)[4719]: starting 0anacron Nov 19 09:01:01 node101 run-parts(/etc/cron.hourly)[4728]: finished 0anacron Nov 19 09:10:01 node101 CROND[4734]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 09:20:02 node101 CROND[4740]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 09:30:01 node101 CROND[4747]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 09:40:01 node101 CROND[4754]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 09:50:01 node101 CROND[4760]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 10:00:01 node101 CROND[4768]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 10:01:01 node101 CROND[4774]: (root) CMD (run-parts /etc/cron.hourly) Nov 19 10:01:01 node101 run-parts(/etc/cron.hourly)[4774]: starting 0anacron Nov 19 10:01:01 node101 run-parts(/etc/cron.hourly)[4783]: finished 0anacron Nov 19 10:10:01 node101 CROND[4787]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 10:20:01 node101 CROND[4793]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 10:30:01 node101 CROND[4799]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 10:40:01 node101 CROND[4805]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 10:50:01 node101 CROND[4813]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 11:00:01 node101 CROND[4819]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 11:01:01 node101 CROND[4824]: (root) CMD (run-parts /etc/cron.hourly) Nov 19 11:01:01 node101 run-parts(/etc/cron.hourly)[4824]: starting 0anacron Nov 19 11:01:01 node101 run-parts(/etc/cron.hourly)[4833]: finished 0anacron Nov 19 11:10:01 node101 CROND[4838]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 11:20:01 node101 CROND[4845]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 11:30:01 node101 CROND[4853]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 11:40:01 node101 CROND[4858]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 11:50:01 node101 CROND[4864]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 12:00:01 node101 CROND[4871]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 19 12:01:01 node101 CROND[4876]: (root) CMD (run-parts /etc/cron.hourly) Nov 19 12:01:01 node101 run-parts(/etc/cron.hourly)[4876]: starting 0anacron Nov 19 12:01:01 node101 run-parts(/etc/cron.hourly)[4885]: finished 0anacron Nov 20 05:41:02 node101 crond[3060]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 72% if used.) Nov 20 05:41:02 node101 crond[3060]: (CRON) INFO (running with inotify support) Nov 20 05:50:01 node101 CROND[3899]: (root) CMD (/usr/lib64/sa/sa1 1 1) Nov 20 05:53:19 node101 run-parts(shell/)[3916]: starting blog.txt Nov 20 05:53:19 node101 run-parts(shell/)[3924]: finished blog.txt Nov 20 05:53:19 node101 run-parts(shell/)[3916]: starting devops.txt Nov 20 05:53:19 node101 run-parts(shell/)[3931]: finished devops.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
7>.系统的计划任务
/etc/crontab
计划任务主配置文件,仅root用户有权限操作。 /etc/cron.d/
存放计划任务的配置文件 /etc/cron.hourly/
定义每小时要执行的任务,如"0anacron"脚本,用来检查anacron今天是否执行 /etc/cron.daily/
定义每天要执行的任务 /etc/cron.weekly/
定义每个星期要执行的任务 /etc/cron.monthly/
定义每个月要执行的任务
[root@node101.yinzhengjie.org.cn ~]# ll /etc/crontab -rw-r--r-- 1 root root 647 Nov 20 06:37 /etc/crontab [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# 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 30 2 * 3-6,12 0 root /bin/tar cf /root/shell.tar /root/shell &> /dev/null */10 * * * * root /root/shell/checkdisk.sh 30 4 1,15 * 7 root /root/shell/f1.sh @reboot root /bin/rm -rf /data/* [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /etc/cron.d/ total 8 -rw-r--r--. 1 root root 128 Apr 11 2018 0hourly -rw------- 1 root root 235 Aug 9 10:54 sysstat [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/cron.d/0hourly # Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/cron.d/sysstat # Run system activity accounting tool every 10 minutes */10 * * * * root /usr/lib64/sa/sa1 1 1 # 0 * * * * root /usr/lib64/sa/sa1 600 6 & # Generate a daily summary of process accounting at 23:53 53 23 * * * root /usr/lib64/sa/sa2 -A [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /etc/cron.hourly/ total 4 -rwxr-xr-x. 1 root root 392 Apr 11 2018 0anacron [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/cron.hourly/0anacron #!/bin/sh # Check whether 0anacron was run today already if test -r /var/spool/anacron/cron.daily; then day=`cat /var/spool/anacron/cron.daily` fi if [ `date +%Y%m%d` = "$day" ]; then exit 0; fi # Do not run jobs when on battery power if test -x /usr/bin/on_ac_power; then /usr/bin/on_ac_power >/dev/null 2>&1 if test $? -eq 1; then exit 0 fi fi /usr/sbin/anacron -s [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /etc/cron.daily/ total 8 -rwx------. 1 root root 219 Oct 31 2018 logrotate -rwxr-xr-x. 1 root root 618 Oct 30 2018 man-db.cron [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/cron.daily/man-db.cron #!/bin/bash if [ -e /etc/sysconfig/man-db ]; then . /etc/sysconfig/man-db fi if [ "$CRON" = "no" ]; then exit 0 fi renice +19 -p $$ >/dev/null 2>&1 ionice -c3 -p $$ >/dev/null 2>&1 LOCKFILE=/var/lock/man-db.lock # the lockfile is not meant to be perfect, it's just in case the # two man-db cron scripts get run close to each other to keep # them from stepping on each other's toes. The worst that will # happen is that they will temporarily corrupt the database [[ -f $LOCKFILE ]] && exit 0 trap "{ rm -f $LOCKFILE ; exit 0; }" EXIT touch $LOCKFILE # create/update the mandb database mandb $OPTS exit 0 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
8>.anacron系统
运行计算机关机时cron不运行的任务,CentOS6以后版本取消anacron服务,由crond服务管理
对笔记本电脑、台式机、工作站、偶尔要关机的服务器及其它不一直开机的系统很重要对很有用
配置文件:/etc/anacrontab,负责执行/etc/ cron.daily /etc/cron.weekly /etc/cron.monthly中系统任务 字段1:如果在这些日子里没有运行这些任务…… 字段2:在重新引导后等待这么多分钟后运行它 字段3:任务识别器,在日志文件中标识 字段4:要执行的任务
由/etc/cron.hourly/0anacron执行
当执行任务时,更新/var/spool/anacron/cron.daily 文件的时间戳
9>.管理临时文件
CentOS6使用/etc/cron.daily/tmpwatch定时清除临时文件 CentOS7使用systemd-tmpfiles-setup服务实现 配置文件: /etc/tmpfiles.d/*.conf /run/tmpfiles.d/*.conf /usr/lib/tmpfiles/*.conf /usr/lib/tmpfiles.d/tmp.conf d /tmp 1777 root root 10d d /var/tmp 1777 root root 30d 命令: systemd-tmpfiles –clean|remove|create configfile
四.at和crontab
一次性作业使用 at 重复性作业使用crontab Create at time crontab -e List at -l crontab -l Details at -c jobnum crontab -l Remove at -d jobnum crontab -r Edit N/A crontab -e 没有被重定向的输出会被邮寄给用户 root能够修改其它用户的作业 温馨提示: 运行结果的标准输出和错误以邮件通知给相关用户 COMMAND > /dev/null COMMAND &> /dev/null 对于cron任务来讲,"%"有特殊用途;如果在命令中要使用"%",则需要转义,将"%"放置于单引号中,则可不用转义
五.小试牛刀
1>.思考
(1) 如何在秒级别运行任务? * * * * * for min in 0 1 2; do echo "hi"; sleep 20; done
(2) 如何实现每7分钟运行一次任务? sleep命令: sleep NUMBER[SUFFIX]... SUFFIX: s: 秒, 默认 m: 分 h: 小时 d: 天
usleep命令:
比sleep更精确,可以达到微妙。
2>.每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@node101.yinzhengjie.org.cn ~]# vim shell/backup_etc.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat shell/backup_etc.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #Email: y1053419035@qq.com #Date: 2019-11-13 #FileName: hello.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: This is the first script #Copyright (C): Original works, no reprint! Otherwise, legal liability will be investigated #******************************************************************** tar Jcf /backup/etcbak-`date -d yesterday +%F-%H`.tar.xz /etc/ &> /dev/null [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# chmod +x shell/backup_etc.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll shell/ total 4 -rwxr-xr-x 1 root root 549 Nov 20 07:58 backup_etc.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# crontab -e crontab: installing new crontab [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# crontab -l 30 1 * * 1-5 /root/shell/backup_etc.sh [root@node101.yinzhengjie.org.cn ~]#
3>.每两小时取出当前系统/proc/meminfo文件中以S或M开头的信息追至/tmp/meminfo.txt文件中
3>.工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就执行wall警报
[root@node101.yinzhengjie.org.cn ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 52403200 1519300 50883900 3% / devtmpfs 3992348 0 3992348 0% /dev tmpfs 4004464 0 4004464 0% /dev/shm tmpfs 4004464 8776 3995688 1% /run tmpfs 4004464 0 4004464 0% /sys/fs/cgroup /dev/sda1 1038336 148556 889780 15% /boot /dev/mapper/centos-home 986351044 3110988 983240056 1% /home tmpfs 800896 0 800896 0% /run/user/0 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# df | sed -nr '/^/dev/sd/s/.* ([0-9]+)%.*/1/p' 15 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# df | sed -nr '/^/dev/mapper/s/.* ([0-9]+)%.*/1/p' 3 1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [ `df | sed -nr '/^/dev/mapper/s/.* ([0-9]+)%.*/1/p' | sort -nr | head -1` -gt 80 ] && wall disk will be full [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#