调试 crontab 作业
我在 Linux 服务器上添加了一个将运行 Java 可执行文件的 crontab 条目。 Java 代码使用自己的类将错误和消息记录到日志文件中。
但是当我在预定时间之后检查日志文件时,没有记录任何消息。应该至少有一条日志消息表明执行已经开始。
所以可能的原因有两个:
- 代码执行了但没有记录;
- 或者,代码根本没有执行。
指定的日志文件具有 chmod 777 权限,因此我猜测这是第二个原因。
为什么 crontab 作业不会在预定时间执行?我该如何调试这个而不发生任何类型的日志记录?
我读过如果有错误 cron 会向用户发送一封电子邮件。如何找出与用户关联的电子邮件地址?
I have added a crontab entry on a Linux server that will run a Java executable. The Java code uses its own class for logging errors and messages into a log file.
But when I checked the log file after the scheduled time, no messages were logged. There should have been at least one log message saying the execution had started.
So there are two possible causes:
- The code executed but didn't log;
- Or, the code didn't execute at all.
The log file specified has chmod 777
permissions so I'm guessing it's the second cause here.
Why wouldn't a crontab job execute at its scheduled time? And how do I debug this without any kind of logging happening?
I have read that if there is an error cron sends an email to the user. How do I find out which email address is associated with the user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以为 cron 作业启用日志记录以跟踪问题。您需要编辑
/etc/rsyslog.conf
或/etc/rsyslog.d/50-default.conf
(在 Ubuntu 上)文件并确保您具有以下内容取消注释行,如果缺少则添加它:然后重新启动 rsyslog 和 cron :
Cron 作业将记录到 /var/log/cron.log 。
You can enable logging for cron jobs in order to track problems. You need to edit the
/etc/rsyslog.conf
or/etc/rsyslog.d/50-default.conf
(on Ubuntu) file and make sure you have the following line uncommented or add it if it is missing:Then restart
rsyslog
andcron
:Cron jobs will log to
/var/log/cron.log
.将 2>&1 附加到 Crontab 命令的末尾。这会将 stderr 输出重定向到 stdout。然后确保您正在记录 crontab 的 Unix 命令。
这将从 Unix 命令捕获任何内容。
一些额外的提示(前几天帮助同事之后......)。通过发出不带参数的命令集写出环境变量。并让 shell 使用 set -x 命令回显每个命令。在脚本问题的顶部;
Append 2>&1 to the end of your Crontab command. This will redirect the stderr output to the stdout. Then ensure you're logging the crontab's Unix command.
This will capture anything from the Unix command.
A couple of additional hints (after helping a colleague the other day ...). Write out the environment variables by issuing the command set with no parameters. And get the shell to echo each command with the set -x command. At the top of your script issue;
对于其他为此苦苦挣扎的人。即使 Cronjobs 不执行 stdoutput,它们也会将自己登录到 /var/mail/{username} 中
For anyone else struggling with this. Cronjobs log themselves in /var/mail/{username} even if they don't do stdoutput
假设手动运行该命令有效,但在 Cron 中无效,则可能是 cron 命令未公开正确的路径。您可以通过运行 crontab -e 然后直接在 cron 选项卡中输入路径来修复此问题:
# 导出路径以便脚本正确运行
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin
Assuming that running the command manually works, but not in Cron, it may be that the correct path is not exposed to the cron command. You can fix this by running crontab -e and then entering the path directly into the cron tab:
# Export the path so that the scripts run correctly
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin
可能导致您出现问题的一件事是
cron
(至少在我使用的发行版 Amazon Linux OS 中)认为时间采用 UTC,因此如果您位于不同的时区(例如 - 03:00) 您可能期望它提前 3 小时运行,而实际上它会运行,而且事实上您没有任何问题。One thing that may be causing your problem is that
cron
(at least in the distribution I am using, Amazon Linux OS) consider times to be in UTC, therefore if you are in a different timezone (e.g. -03:00) you may be expecting it to run 3 hours in advance that it actually will and in fact you don't have any problem.检查您是否确实格式化了必须运行良好的时间。
例如,而不是
可能是这样的:
并且您可能期望它每分钟运行一次。并且也不会生成任何日志。
Check if you really formatted the time when it has to run well.
For example instead of
might be this:
and you might be expecting it to run every minute. And also no logs are generated.