WTF?! My cronjob doesn’t run?!
Here’s a checklist guide to debug not running cronjobs:
- Is the Cron daemon running?
- Run
ps ax | grep cronand look for cron. - Debian:
service cron startorservice cron restart
- Run
- Is cron working?
* * * * * /bin/echo "cron works" >> /file- Syntax correct? See below.
- Is the command working standalone?
- Check if the script has an error, by doing a dry run on the CLI
- when testing your command, test as the user whose crontab you are editing, which might not be your login or root
- Can cron run your job?
- Check
/var/log/cron.logor/var/log/messagesfor errors. - Ubuntu:
grep CRON /var/log/syslog - Redhat:
/var/log/cron
- Check
- Check permissions
- set executable flag on the command:
chmod +x /var/www/app/cron/do-stuff.php - if you redirect the output of your command to a file, verify you have permission to write to that file/directory
- set executable flag on the command:
- Check paths
- check she-bangs / hashbangs line
- do not rely on environment variables like PATH, as their value will likely not be the same under cron as under an interactive session
- Don’t Suppress Output, while debugging
- commonly used is this suppression:
30 1 * * * command > /dev/null 2>&1 - re-enable the standard output or standard error message output
- commonly used is this suppression:
Still not working? Yikes!
- Raise the cron debug level
- Debian
- in
/etc/default/cron - set
EXTRA_OPTS="-L 2" service cron restarttail -f /var/log/syslogto see the scripts executed
- in
- Ubuntu
- in
/etc/rsyslog.d/50-default.conf - add or comment out line
cron.crit /var/log/cron.log - reload logger
sudo /etc/init.d/rsyslog reload - re-run cron
- open
/var/log/cron.logand look for detailed error output
- in
- Reminder: deactivate log level, when you are done with debugging
- Debian
- Run cron and check log files again
Cronjob Syntax
# Minute Hour Day of Month Month Day of Week User Command
本文提供了一套全面的检查清单来帮助您诊断并解决不运行的Cron作业问题。内容包括验证Cron守护进程是否运行、测试Cron作业是否正常工作、检查命令语法、单独测试脚本、权限及路径问题等。
1586

被折叠的 条评论
为什么被折叠?



