在线工具

crontab 在线网站: https://tooltt.com/crontab/c/56.html

在线 cron 表达式生成: https://cron.qqe2.com/

安装 cron

CentOS 默认自带,如需安装:

1
2
3
4
yum -y install vixie-cron

# 验证
info crontab

基本使用命令

1
2
3
4
5
6
7
8
crontab -u    # 设定某个用户的 cron 服务,一般 root 用户在执行这个命令的时候需要此参数
crontab -l # 列出某个用户 cron 服务的详细内容
crontab -r # 删除某个用户的 cron 服务
crontab -e # 编辑某个用户的 cron 服务

# 示例:
crontab -u root -l # root 查看自己的 cron 设置
crontab -u fred -r # root 想删除 fred 的 cron 设置

查看日志

1
2
3
4
5
# 查看执行日志
tail -f /var/log/cron

# 查看任务执行日志(排错用,root 替换为对应用户名)
tail -f /var/spool/mail/root

服务管理

1
2
3
4
5
6
7
8
9
10
11
12
/sbin/service crond start      # 启动服务 
/sbin/service crond stop # 关闭服务
/sbin/service crond restart # 重启服务
/sbin/service crond reload # 重新载入配置
/sbin/service crond status # 查看状态

# 或者使用简化命令
service crond start
service crond stop
service crond restart
service crond reload
service crond status

添加到开机服务

/etc/rc.d/rc.local 这个脚本的末尾加上:

1
/sbin/service crond start

示例

1
2
3
4
5
6
*/5 * * * * Command           # 每 5 分钟执行一次命令
5 * * * * Command # 每小时的第 5 分钟执行一次命令
30 18 * * * Command # 每天下午的 6:30 执行一次命令
30 7 8 * * Command # 每月 8 号的 7:30 分执行一次命令
30 5 8 6 * Command # 每年的 6 月 8 日 5:30 执行一次命令
30 6 * * 0 Command # 每星期日的 6:30 执行一次命令

特殊字符串

字符串含义
@reboot开机时运行一次
@yearly每年运行一次,等同于 0 0 1 1 *
@annually@yearly
@monthly每月运行一次,等同于 0 0 1 * *
@weekly每周运行一次,等同于 0 0 * * 0
@daily每天运行一次,等同于 0 0 * * *
@midnight@daily
@hourly每小时运行一次,等同于 0 * * * *

配置完成后,会在开机后进行启动。如果需要延时启动,可以参考:

1
@reboot sleep 300 && /home/start.sh

注意事项

crontab 中 % 是有特殊意义的,如果使用到了 %,需要进行转义:\%

常见问题

问题 1:cron 表达式格式不同

cron 表达式和开发中常用 6/7 位的方式不同。假如想设置每分钟执行,0/1 * * * * 是不对的,需要以 * 开头:*/1 * * * *

问题 2:没有任务执行日志

一般是因为 postfix.service 没有或者没启动,对应安装下即可:

1
2
3
4
# 检查状态
systemctl status postfix.service

# 如果不想发送邮件,将 postfix 服务停掉即可

问题 3:not found command

一般是因为在 crontab 中没有指定环境。

例如任务将要执行的脚本中使用到了 kubectl,但是 kubectl/usr/local/bin 下,需要明确指出,如下:

1
2
3
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO=root

问题 4:postfix 启动报错

报错信息:newaliases: fatal: parameter inet_interfaces: no local interface found for ::1

编辑 /etc/postfix/main.cf

1
vi /etc/postfix/main.cf

将配置:

1
2
inet_interfaces = localhost
inet_protocols = all

改成:

1
2
inet_interfaces = all
inet_protocols = all

接着重新启动,如果又出现以下信息:

1
2
3
Apr 28 09:09:08 PaulV1 postfix[23919]: postsuper: fatal: scan_dir_push: open directory defer: Permission denied
Apr 28 09:09:08 PaulV1 postfix/postsuper[23952]: fatal: scan_dir_push: open directory defer: Permission denied
Apr 28 09:09:10 PaulV1 postfix/postfix-script[23953]: fatal: Postfix integrity check failed!

这是因为启动时邮件服务对系统目录没有权限导致的,更改相关目录的所有者,然后重新启动:

1
chown -R postfix /var/spool/postfix/

如果启动的时候显示还有一个目录没有权限:

1
2
3
Apr 28 09:21:57 PaulV1 postfix/master[24146]: fatal: open lock file /var/lib/postfix/master.lock: cannot open file: Permission denied
Apr 28 09:21:58 PaulV1 postfix/master[24145]: fatal: daemon initialization failure
Apr 28 09:21:59 PaulV1 postfix/postfix-script[24147]: fatal: mail system startup failed

执行:

1
chown -R postfix /var/lib/postfix/

最后才能启动成功。