我想知道如何在每次执行时准确地看到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
其他回答
* * * * * myjob.sh >> /var/log/myjob.log 2>&1
是否将cron作业的所有输出记录到/var/log/myjob.log
你可以用mail发送电子邮件。大多数系统将通过电子邮件将未处理的cron作业输出发送给root或相应的用户。
默认cron日志到/var/log/syslog,这样你就可以看到cron相关的条目:
grep CRON /var/log/syslog
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
如果你用sudo运行一些命令,它将不允许。须藤需要洗漱。
这是我的代码:
* * * * * your_script_fullpath >> your_log_path 2>&1
使用命令crontab -e,然后将cron作业编辑为
* * * * * /path/file.sh > /pathToKeepLogs/logFileName.log 2>&1
这里,2>&1表示标准错误(2>)被重定向到由标准输出(&1)指向的相同文件描述符。
推荐文章
- 如何配置slf4j-simple
- 如何在Python中记录源文件名称和行号
- MongoDB记录所有查询
- 如何禁用标准错误流的日志记录?
- 如何跟踪log4net问题
- 在没有事件源注册的情况下写入Windows应用程序事件日志
- Python日志记录不输出任何东西
- IE9是否支持console.log,它是一个真实的功能吗?
- 有nginx access_log和error_log日志的STDOUT和STDERR的主进程
- 在Python Django中运行单元测试时,如何禁用日志记录?
- 哪里是logging.config.dictConfig的完整示例?
- Log4j日志层次顺序
- 如何通过脚本创建crontab
- 如何在MySQL中查看日志文件?
- 截断SQL Server日志文件的命令是什么?