调试crontab作业 [英] Debugging crontab jobs
问题描述
我在将运行Java可执行文件的Linux服务器上添加了crontab条目. Java代码使用其自己的类将错误和消息记录到日志文件中.
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:
- 代码已执行但未记录;
- 或者,代码根本没有执行.
指定的日志文件具有chmod 777
权限,所以我想这是这里的第二个原因.
The log file specified has chmod 777
permissions so I'm guessing it's the second cause here.
为什么crontab作业不按计划的时间执行?以及如何在不进行任何日志记录的情况下调试它?
Why wouldn't a crontab job execute at its scheduled time? And how do I debug this without any kind of logging happening?
我已阅读到,如果发生错误,cron会向用户发送电子邮件.如何找出与该用户关联的电子邮件地址?
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?
推荐答案
您可以启用cron作业的日志记录以跟踪问题.您需要编辑/etc/rsyslog.conf or /etc/rsyslog.d/50-default.conf
(在Ubuntu上)文件,并确保未注释以下行;如果缺少,请添加以下行:
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:
cron.* /var/log/cron.log
然后重新启动rsyslog
和cron
:
sudo service rsyslog restart
sudo service cron restart
Cron作业将登录到/var/log/cron.log
.
Cron jobs will log to /var/log/cron.log
.
这篇关于调试crontab作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!