Cron 进程
RedHat/CentOS
service crond status
Ubuntu
service cron status
Cron 语法
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute
# 每 30 分钟执行一次 logrotate
*/30 * * * * /usr/sbin/logrotate /etc/logrotate.d/nginx >/dev/null 2>&1
特殊字符
字符 | 作用 | 举例 | 说明 |
---|---|---|---|
* |
任何可能值 | * * * * * echo hello |
每分钟打印一次 “hello” |
- |
范围 | 1-6 * * * * echo hello |
每小时的1到6分钟,每分钟都会打印 “hello” |
/ |
间隔 | */10 * * * * echo hello |
每10分钟打印一次 “hello” |
, |
列举 | 1,5 * * * * echo hello |
每小时的1,5分时,打印 “hello” |
列出 Crontab 中的定时任务
# 列出当前用户的定时任务
crontab -l
# 列出用户 peter 的定时任务
crontab -u peter -l
编辑 Crontab
crontab -e
预定义的 Cron 目录
# hourly, daily, weekly and monthly cron jobs
cd /etc/cron.daily/
cd /etc/cron.hourly/
cd /etc/cron.weekly/
cd /etc/cron.monthly/
# package specific cron jobs
cd /etc/cron.d/
# user specific cron jobs
cd /var/spool/cron/
What’s the difference between /etc/cron.d and /var/spool/cron?
-
/var/spool/cron
is where the individual user crontabs live. As user,crontab -e
edits the corresponding file in/var/spool/cron
. -
/etc/cron.d
is a directory that is scanned for modular crontab files. The syntax is slightly different for files in that directory. The cron entries have an additional field for a user to run the cron entries as. This is the same as a systemwide/etc/crontab
file.
cronjob 执行日志
参考文档: CronJob not running
检查 cronjob 中的命令是否有执行,可以查看以下日志文件
- RedHat 检查
/var/log/cron
- Ubuntu 检查
grep CRON /var/log/syslog
- 其它系统检查
/var/log/cron.log
or/var/log/messages