我想知道如何在每次执行时准确地看到cron作业正在做什么。日志文件位于哪里?或者我可以把输出发送到我的邮箱吗?我已经设置了电子邮件地址,当cron作业运行时发送日志,但我还没有收到任何东西。
当前回答
至少有三种不同类型的日志记录:
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
其他回答
这是我的代码:
* * * * * your_script_fullpath >> your_log_path 2>&1
默认cron日志到/var/log/syslog,这样你就可以看到cron相关的条目:
grep CRON /var/log/syslog
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
如果你用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/
如果你仍然想检查你的cron作业,你应该提供一个有效的 在cPanel中设置Cron作业时使用电子邮件帐户。
当您指定一个有效的电子邮件时,您将收到所执行的cron作业的输出。因此,您将能够检查它,并确保一切都已正确执行。注意,如果cron作业命令没有输出,您将不会收到电子邮件。
请记住,每个执行的cron作业都会收到一封电子邮件。这可能会淹没你的收件箱,以防你的crons运行太频繁
推荐文章
- 在log4j中,在记录日志之前检查isDebugEnabled是否能提高性能?
- 将Node.js配置为记录到文件而不是控制台
- 为什么logcat没有显示任何东西在我的Android?
- 在Python中记录未捕获的异常
- 重定向复制的标准输出到日志文件从bash脚本本身
- Python日志:使用时间格式的毫秒
- IE8的console.log怎么了?
- 如何从Kubernetes复制控制器的所有pod中获取日志?
- Spring RestTemplate -如何启用请求/响应的完整调试/日志记录?
- 每周测试一次cron作业
- Crontab每周的日期语法
- 子进程命令的实时输出
- 我怎么能让cron作业每30分钟运行一次?
- 如何写一个文件,使用日志Python模块?
- 如何模拟环境cron执行脚本?