我想知道如何在每次执行时准确地看到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

如果你用sudo运行一些命令,它将不允许。须藤需要洗漱。

在Ubuntu上,你可以让CRON .log文件只包含CRON条目。

取消/etc/rsyslog.d/50-default.conf文件中提到cron的行注释:

#  Default rules for rsyslog.
#

#                       For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
#cron.*                          /var/log/cron.log

保存并关闭文件,然后重新启动rsyslog服务:

sudo systemctl restart rsyslog

你现在可以看到cron日志条目在它自己的文件中:

sudo tail -f /var/log/cron.log

示例输出:

Jul 18 07:05:01 machine-host-name CRON[13638]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

但是,您不会看到关于/etc/cron中实际运行的脚本的更多信息Daily或/etc/cron.每小时一次,除非这些脚本将输出直接输出到cron.log(或者其他一些日志文件)。

如果你想验证crontab是否正在运行,而不需要在crontab .log或syslog中搜索它,创建一个crontab,将输出重定向到你选择的日志文件-类似于:

# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
30 2 * * 1 /usr/local/sbin/certbot-auto renew >> /var/log/le-renew.log 2>&1

步骤从:https://www.cyberciti.biz/faq/howto-create-cron-log-file-to-log-crontab-logs-in-ubuntu-linux/

* * * * * myjob.sh >> /var/log/myjob.log 2>&1

是否将cron作业的所有输出记录到/var/log/myjob.log

你可以用mail发送电子邮件。大多数系统将通过电子邮件将未处理的cron作业输出发送给root或相应的用户。

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

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

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