Linux 调试 crontab 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4883069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:16:28  来源:igfitidea点击:

Debugging crontab jobs

linuxcron

提问by naiquevin

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.

我在将运行 Java 可执行文件的 Linux 服务器上添加了一个 crontab 条目。Java 代码使用自己的类将错误和消息记录到日志文件中。

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:

所以有两个可能的原因:

  1. The code executed but didn't log;
  2. Or, the code didn't execute at all.
  1. 代码执行但没有记录;
  2. 或者,代码根本没有执行。

The log file specified has chmod 777permissions so I'm guessing it's the second cause here.

指定的日志文件具有chmod 777权限,所以我猜这是这里的第二个原因。

Why wouldn't a crontab job execute at its scheduled time? And how do I debug this without any kind of logging happening?

为什么 crontab 作业不会在预定时间执行?以及如何在不进行任何日志记录的情况下进行调试?

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 会向用户发送电子邮件。如何找出与用户关联的电子邮件地址?

回答by Karl

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.

将 2>&1 附加到 Crontab 命令的末尾。这会将 stderr 输出重定向到 stdout。然后确保您正在记录 crontab 的 Unix 命令。

0 0,12 1 */2 * /sbin/ping -c 192.168.0.1; ls -la >>/var/log/cronrun 2>&1

This will capture anything from the Unix command.

这将从 Unix 命令捕获任何内容。

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;

一些额外的提示(前几天帮助了一位同事之后......)。通过发出不带参数的命令集写出环境变量。并使用 set -x 命令让 shell 回显每个命令。在脚本问题的顶部;

set
set -x

回答by Darius.V

Check if you really formatted the time when it has to run well.

检查您是否真的格式化了必须运行良好的时间。

For example instead of

例如代替

*/1 * * * * echo 'debug' > /home/glab/change_branch.log

might be this:

可能是这样的:

1 * * * * echo 'debug' > /home/glab/change_branch.log

and you might be expecting it to run every minute. And also no logs are generated.

您可能希望它每分钟运行一次。并且也不会生成日志。

回答by Renato Gama

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.

可能导致您出现问题的一件事是cron(至少在我使用的发行版 Amazon Linux 操作系统中)考虑时间是 UTC,因此如果您在不同的时区(例如 -03:00),您可能会期待它提前 3 小时运行,它实际上会并且实际上您没有任何问题。

回答by Mehdi Yedes

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 作业启用日志记录以跟踪问题。您需要编辑/etc/rsyslog.conf or /etc/rsyslog.d/50-default.conf(在 Ubuntu 上)文件并确保取消注释以下行,如果缺少则添加它:

cron.*                         /var/log/cron.log

Then restart rsyslogand cron:

然后重新启动rsyslogcron

sudo service rsyslog restart
sudo service cron restart

Cron jobs will log to /var/log/cron.log.

Cron 作业将登录到/var/log/cron.log.

回答by user189271

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:

假设手动运行命令有效,但在 Cron 中无效,可能是正确的路径没有暴露给 cron 命令。您可以通过运行 crontab -e 然后直接在 cron 选项卡中输入路径来解决此问题:

# 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

# 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

回答by Michael Eliot

For anyone else struggling with this. Cronjobs log themselves in /var/mail/{username} even if they don't do stdoutput

对于其他为此而苦苦挣扎的人。即使不执行 stdoutput,Cronjobs 也会将自己登录到 /var/mail/{username}