Skip to content

Instantly share code, notes, and snippets.

@ibreathebsb
Last active September 14, 2018 10:36
Show Gist options
  • Save ibreathebsb/c5a316124758c6d3a80dc515a5327ece to your computer and use it in GitHub Desktop.
Save ibreathebsb/c5a316124758c6d3a80dc515a5327ece to your computer and use it in GitHub Desktop.

at

只在某个时间点只执行一次的任务:

at now + 1minutes

crontab

# cron.hourly cron.daily cron.weekly cron.monthly 目录下的脚本对应着不同的执行周期
# crontab -e 编辑
# crontab -l 展示列表
# crontab -r 清空

-rw-r--r--   1 root root      401 May 30  2017 anacrontab
drwxr-xr-x   2 root root     4096 Sep 14 09:10 cron.d
drwxr-xr-x   2 root root     4096 Sep 14 09:10 cron.daily
drwxr-xr-x   2 root root     4096 Sep 14 09:10 cron.hourly
drwxr-xr-x   2 root root     4096 Sep 14 09:10 cron.monthly
-rw-r--r--   1 root root      722 May  3  2015 crontab
drwxr-xr-x   2 root root     4096 Sep 14 09:10 cron.weekly

isaac@debian:/etc$ crontab -l
# 分 | 小时 | 天 | 月 | 周几 | 执行的任务 
# 每分钟向/home/isaac/test文件中添加test

*/1 * * * * echo test >> /home/isaac/test

# 关于时间的语法
# 单独一个数字不用说了
# 时间段 a-b
# 某几个时间a,b,c
# 任意时间 *
# 制定时间间隔 */interval

# 一段时间后

isaac@debian:/etc$ cat /home/isaac/test 
test
test
test
test

# 取消任务

crontab -e  然后删除对应的行就ok啦

# 系统任务

直编辑 /etc/crontab

anacron

由于主机并不是总是处于开机状态,所以crontab安排的定时任务,可能在主机关机时没有得到执行,如何解决?

anacron会在启动时检查上次crontab执行的时间,如果超过了规定的执行周期就执行一次

执行顺序,查看anacrontab,中的任务,可以多看到有daily monthly... 以daily为例,若当前时间和上次执行的时间超过了1天,那么会执行/etc/cron.daily下的所有脚本 注意第一个脚本0anacron之所以用0开头,是为了保证脚本最先执行

isaac@debian:/etc$ cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/root
LOGNAME=root

# These replace cron's entries
# 周期 延迟时间 任务名称 执行命令
1	5	cron.daily	run-parts --report /etc/cron.daily
7	10	cron.weekly	run-parts --report /etc/cron.weekly
@monthly	15	cron.monthly	run-parts --report /etc/cron.monthly


isaac@debian:/etc$ ls -l /etc/cron.daily/
total 44
-rwxr-xr-x 1 root root  311 May 30  2017 0anacron
-rwxr-xr-x 1 root root 1474 Jul 14  2017 apt-compat
-rwxr-xr-x 1 root root  355 Oct 25  2016 bsdmainutils
-rwxr-xr-x 1 root root  384 Dec 13  2012 cracklib-runtime
-rwxr-xr-x 1 root root 1597 Feb 23  2017 dpkg
-rwxr-xr-x 1 root root 4125 Feb 10  2018 exim4-base
-rwxr-xr-x 1 root root   89 May  6  2015 logrotate
-rwxr-xr-x 1 root root 1065 Dec 13  2016 man-db
-rwxr-xr-x 1 root root  249 May 17  2017 passwd
-rwxr-xr-x 1 root root  349 Feb  1  2018 quota
isaac@debian:/etc$ cat /etc/cron.daily/0anacron 
#!/bin/sh
#
# anacron's cron script
#
# This script updates anacron time stamps. It is called through run-parts
# either by anacron itself or by cron.
#
# The script is called "0anacron" to assure that it will be executed
# _before_ all other scripts.

test -x /usr/sbin/anacron || exit 0
anacron -u cron.daily


# 手动执行anacron,可以指定只执行单个任务
# -f 强制执行
# -u 只更新时间
# -n 马上执行忽略任务里面的delay配置
# -s 排队任务等待当前cron执行完成后再执行

每个任务上次的执行时间记录放在/var/spool/anacron

root@debian:/var/spool/anacron# ls
cron.daily  cron.monthly  cron.weekly

root@debian:/var/spool/anacron#sudo ls -l /var/spool/anacron/ | sed '1d' |  awk '{print $9}' | xargs -n 1 cat
20180914
20180820
20180912

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment