调试 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
或 /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屋!