我想知道如何在每次执行时准确地看到cron作业正在做什么。日志文件位于哪里?或者我可以把输出发送到我的邮箱吗?我已经设置了电子邮件地址,当cron作业运行时发送日志,但我还没有收到任何东西。


当前回答

使用命令crontab -e,然后将cron作业编辑为

* * * * * /path/file.sh > /pathToKeepLogs/logFileName.log 2>&1

这里,2>&1表示标准错误(2>)被重定向到由标准输出(&1)指向的相同文件描述符。

其他回答

至少有三种不同类型的日志记录:

The logging BEFORE the program is executed, which only logs IF the cronjob TRIED to execute the command. That one is located in /var/log/syslog, as already mentioned by @Matthew Lock. The logging of errors AFTER the program tried to execute, which can be sent to an email or to a file, as mentioned by @Spliffster. I prefer logging to a file, because with email THEN you have a NEW source of problems, and its checking if email sending and reception is working perfectly. Sometimes it is, sometimes it's not. For example, in a simple common desktop machine in which you are not interested in configuring an smtp, sometimes you will prefer logging to a file: * * * * COMMAND_ABSOLUTE_PATH > /ABSOLUTE_PATH_TO_LOG 2>&1 I would also consider checking the permissions of /ABSOLUTE_PATH_TO_LOG, and run the command from that user's permissions. Just for verification, while you test whether it might be a potential source of problems. The logging of the program itself, with its own error-handling and logging for tracking purposes.

There are some common sources of problems with cronjobs: * The ABSOLUTE PATH of the binary to be executed. When you run it from your shell, it might work, but the cron process seems to use another environment, and hence it doesn't always find binaries if you don't use the absolute path. * The LIBRARIES used by a binary. It's more or less the same previous point, but make sure that, if simply putting the NAME of the command, is referring to exactly the binary which uses the very same library, or better, check if the binary you are referring with the absolute path is the very same you refer when you use the console directly. The binaries can be found using the locate command, for example:

$locate python

请确保您将引用的二进制文件与您在shell中调用的二进制文件完全相同,或者只是在shell中使用您计划放入cronjob的绝对路径再次测试。

另一个常见的问题来源是cronjob中的语法。记住,有一些特殊字符可以用于列表(逗号)、定义范围(破折号-)、定义范围的增量(斜杠)等。来看看: http://www.softpanorama.org/Utilities/cron.shtml

使用命令crontab -e,然后将cron作业编辑为

* * * * * /path/file.sh > /pathToKeepLogs/logFileName.log 2>&1

这里,2>&1表示标准错误(2>)被重定向到由标准输出(&1)指向的相同文件描述符。

这是我的代码:

* * * * * your_script_fullpath >> your_log_path 2>&1

如果你仍然想检查你的cron作业,你应该提供一个有效的 在cPanel中设置Cron作业时使用电子邮件帐户。

当您指定一个有效的电子邮件时,您将收到所执行的cron作业的输出。因此,您将能够检查它,并确保一切都已正确执行。注意,如果cron作业命令没有输出,您将不会收到电子邮件。

请记住,每个执行的cron作业都会收到一封电子邮件。这可能会淹没你的收件箱,以防你的crons运行太频繁

默认cron日志到/var/log/syslog,这样你就可以看到cron相关的条目:

grep CRON /var/log/syslog

https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log