安装cron(centos默认自带) 1 2 3 yum -y install vixie-cron info crontab: 验证
一些基本使用命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数 crontab -l //列出某个用户cron服务的详细内容 crontab -r //删除没个用户的cron服务 crontab -e //编辑某个用户的cron服务 比如说root查看自己的cron设置:crontab -u root -l 再例如,root想删除fred的cron设置:crontab -u fred -r 在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab -u root -e 查看执行日志: tail -f /var/log/cron 查看任务执行日志(排错用,root替换为对应用户名): tail -f /var/spool/mail/root /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这个脚本的末尾加上: /sbin/service crond start 示例(在线cron: https://cron.qqe2.com/) */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执行一次命令 参数方式 string meaning ------ ----------- @reboot Run once, at startup. @yearly Run once a year, "0 0 1 1 *". @annually (same as @yearly) @monthly Run once a month, "0 0 1 * *". @weekly Run once a week, "0 0 * * 0". @daily Run once a day, "0 0 * * *". @midnight (same as @daily) @hourly Run once an hour, "0 * * * *". 配置完成后,会在开机后进行启动,如果需要延时启动,可以参考: @reboot sleep 300 && /home/start.sh crontab中%是有特殊意义的,如果使用到了%,需要进行转义:\%
问题1: cron表达式和开发中常用6/7位的方式不同,假如像设置每分钟执行,0/1 * * * *是不对的,需要是*开头*/1 * * * * **问题2: 没有任务执行日志 ** 一般是因为postfix.service没有或者没启动, 对应安装下即可
然后再进行检查systemctl status postfix.service
如果不想发送邮件,将postfix服务停掉即可
**问题2: 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 MAILO=root
问题3: postfix 启动报错newaliases: fatal: parameter inet_interfaces: no local interface found for ::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/
最后。才启动成功了。