本文共 5194 字,大约阅读时间需要 17 分钟。
一、计划任务的重要性
说明:作为系统运维人员都清楚,在Linux下定义重复性的任务,我们一般会采用crontab来进行:crontab这个指令所设置的工作将会循环的一直进行下去!可循环的时间为分钟、小时、每日、每月、每周。
二、crontab计划任务:
1)常见用法:
crontab命令的作用和用法如下:
描述:为每个用户维护周期性的计划任务文件
用法:crontab [-u 用户][-l|-r|-e]
-u <user> 指定某个用户的计划任务(只有root用户才有这个权限)
-e 编辑用户的计划任务
-l 列出用户的计划任务
-r 删除用户的计划任务
-i 删除时进行交互式操作
2)用户的cron计划任务文件格式含义如下:
* * * * * 指令
分 时 日 月 周 指令
0-59 0-24 1-31 1-12 0-7
3)如果需要指定时间段:
a:可以使用横杠(-)表示一段连续的时间
b: 使用逗号(,)表示若个不连续的时间
c: 使用星号(*)表示所有的时间,使用除号(/)表示间隔时间
4)crontab的常规用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # 列出用户root的计划任务 [root@localhost ~] # crontab -l * /30 * * * * ntpdate -u s2m. time .edu.cn > /dev/null 2>&1 # 列出用户wanlong的计划任务 [root@localhost ~] # crontab -l -u wanlong 0 * /5 * * * /usr/bin/ping 127.0.0.1 > /dev/null # 计划任务的配置文件是保存在/var/spool/cron/下,只要有计划任务的用户都对应有配置文件 [root@localhost ~] # cd /var/spool/cron/ [root@localhost cron ] # ls root wanlong [root@localhost cron ] # cat wanlong 0 * /5 * * * /usr/bin/ping 127.0.0.1 > /dev/null [root@localhost cron ] # cat root * /30 * * * * ntpdate -u s2m. time .edu.cn > /dev/null 2>&1 说明:可以发现 /var/spool/cron/wanlong 与 crontab -l -u wanlong显示的结果是一直的,然后我们测试下直接修改配置文件,看计划任务是否生效 [root@localhost cron ] # echo "0 */2 * * * /usr/bin/ping 127.0.0.1 >/dev/null" >>/var/spool/cron/root [root@localhost cron ] # crontab -l * /30 * * * * ntpdate -u s2m. time .edu.cn > /dev/null 2>&1 0 * /2 * * * /usr/bin/ping 127.0.0.1 > /dev/null 说明:以上可知,直接修改配置文件,仍旧生效。 |
5)如何实现秒级别crontab
方法1:
1 2 | # crontab -l * * * * * for min in 0 1 2; do echo "hi" ; sleep 20; done |
方法2:
1 2 3 4 5 6 7 8 9 10 | # vim seconds.sh #!/bin/bash #created by molewan while : do /test .sh sleep 7 done # chmod 755 seconds.sh # nohup bash seconds.sh & |
补充知识:
1 2 3 | sleep 用法格式: sleep NUMBER[SUFFIX] SUFFIX值可以为s(秒,默认值)、m(分钟)、h(小时)、d(天数) |
6)如何每7分钟运行
不能被60整除的,不能精确做到真正运行
错误做法:*/7 * * * *
1 2 3 4 5 6 7 | #!/bin/bash #created by molewan while : do /test .sh sleep 7 done |
7)练习:
1、每4小时备份一次/etc目录至/backup目录中,保存文件名称格式为“etc-yyyy-mm-dd-HH.tar.xz”
1 2 | # crontab -l * * /4 * * * tar -cJPf /backup/etc- $( date +%F-%k). tar .xz /etc > /dev/null 2>&1 |
2、每周2,4,7备份/var/log/messages文件至/logs目录中,文件名为“messages-yyyymmdd”;
1 2 | # crontab -l * * * * 2,4,7 tar -cf /logs/messages- $( date +%y%m%d) |
3、每两小时取出当前系统/proc/meminfo文件中以S或M开头的信息追加/tmp/meminfo.txt文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@zabbix ~] # egrep -i "^(S|M)" /proc/meminfo MemTotal: 5947112 kB MemFree: 1669468 kB MemAvailable: 2726264 kB SwapCached: 116600 kB Mlocked: 0 kB SwapTotal: 4194300 kB SwapFree: 3288708 kB Mapped: 117324 kB Shmem: 285240 kB Slab: 188640 kB SReclaimable: 133080 kB SUnreclaim: 55560 kB # crontab -e * * /2 * * * egrep -i "^(S|M)" /proc/meminfo >> /tmp/meminfo .txt |
4、工作日时间内,每小时执行一次“ip addr show”
1 | * * /1 * * 1-5 /usr/sbin/ip addr show |
三、其它crontab说明
a、在 Linux 下面的 crontab 会自动的帮我们每分钟重新读取一次 /etc/crontab 的例行工作事项;
b、但是某些原因或者是其他的 Unix 系统中, 由于 crontab 是读到内存当中的,所以在你修改完 /etc/crontab 之后,可能并不会马上执行,这个时候请重新启动crond服务 ;
c、周与日月不可同时并存
1 2 3 | [root@zabbix ~] # systemctl restart crond.service [root@zabbix ~] # echo $? 0 |
执行前先看下,是否有计划任务正在执行
可唤醒停机期间的工作任务:
1 2 3 4 5 6 7 8 9 10 | anacron: [root@zabbix ~] # 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 |
# 上面的语法在检测前一次执行anacron时的时间戳
1 2 3 4 5 6 7 | # 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
# 所以其实也仅是执行anacron -s的指令!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@zabbix ~] # anacron -h Usage: anacron [options] [job] ... anacron -T [-t anacrontab- file ] Options: -s Serialize execution of jobs -f Force execution of jobs, even before their time -n Run jobs with no delay, implies -s -d Don't fork to the background -q Suppress stderr messages, only applicable with -d -u Update the timestamps without actually running anything -V Print version information -h Print this message -t < file > Use alternative anacrontab -T Test an anacrontab -S < dir > Select a different spool directory See the anacron(8) manpage for more details. |
选项与参数:
-s : 开始一连续的执行各项工作 (job),会依据时间记录文件的数据判断是否进行;
-f : 强制进行,而不去判断时间记录文件的时间戳记;
-n : 立刻进行未进行的任务,而不延迟 ( delay) 等待时间;
-u : 仅更新时间记录文件的时间戳记,不进行任何工作。
job :由 /etc/anacrontab 定义的各项工作名称
在我们的 CentOS 中,anacron 的进行其实是在每个小时都会被抓出来执行一次, 但是为了担心 anacron 误判时间参数, 因此 /etc/cron.hourly/ 里面的 anacron 才会在文件名之前
加个 0 ( 0anacron),让 anacron 最先进行.就是为了让时间戳记先更新! 以避免 anacron 误判 crontab 尚未进行任何工作的意思。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 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 15cron.dailynice run-parts /etc/cron .daily 725cron.weeklynice run-parts /etc/cron .weekly @monthly 45cron.monthlynice run-parts /etc/cron .monthly |