I want to know how I can see exactly what the cron jobs are doing on each execution. Where are the log files located? Or can I send the output to my email? I have set the email address to send the log when the cron job runs but I haven't received anything yet.
我想知道在每个执行过程中,cron作业是怎么做的。日志文件位于何处?或者我可以把输出发送到我的邮箱?我已经设置了邮件地址,在cron作业运行时发送日志,但是我还没有收到任何东西。
8 个解决方案
#1
228
* * * * * myjob.sh >> /var/log/myjob.log 2>&1
will log all output from the cron job to /var/log/myjob.log
将cron作业的所有输出记录到/var/log/myjob.log ?
You might use mail
to send emails. Most systems will send unhandled cron
job output by email to root or the corresponding user.
你可以用邮件发送电子邮件。大多数系统将通过电子邮件发送未处理的cron作业输出到根或对应的用户。
#2
46
By default cron logs to /var/log/syslog so you can see cron related entries by using:
默认情况下,cron日志到/var/log/syslog,以便您可以通过以下方式查看cron相关条目:
grep CRON /var/log/syslog
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
#3
8
Here is my code:
这是我的代码:
* * * * * your_script_fullpath >> your_log_path 2>&1
#4
8
There are at least three different types of logging:
至少有三种不同类型的日志记录:
-
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.
程序执行前的日志记录,只有当cronjob试图执行命令时才会记录日志。这个位于/var/log/syslog中,就像@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:
程序尝试执行后的错误记录,可以发送到电子邮件或文件,如@Spliffster所述。我喜欢登录到一个文件,因为有了电子邮件,你就有了一个新的问题来源,它检查邮件发送和接收是否正常工作。有时是,有时不是。例如,在一个简单的普通桌面机器中,您对配置smtp不感兴趣,有时您更喜欢将日志记录到文件中:
* * * * 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.
- 我还将考虑检查/ABSOLUTE_PATH_TO_LOG的权限,并从该用户的权限运行该命令。只是为了验证,当您测试它是否可能成为问题的潜在来源时。
- 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:
在cronjobs中有一些常见的问题来源:*要执行的二进制文件的绝对路径。当您从shell中运行它时,它可能会起作用,但是cron进程似乎使用了另一个环境,因此如果您不使用绝对路径,那么它并不总是能找到二进制文件。*二进制文件所使用的库。以前或多或少相同的点,但确保,如果简单地把命令的名称,指的是完全二进制使用完全相同的库,或者更好,检查如果您所指的是二进制的绝对路径是相同的你直接使用控制台时参考。可以使用locate命令找到二进制文件,例如:
$locate python
Be sure that the binary you will refer, is the very same the binary you are calling in your shell, or simply test again in your shell using the absolute path that you plan to put in the cronjob.
请确保您将引用的二进制文件与您在shell中调用的二进制文件是相同的,或者使用您计划放入cronjob的绝对路径在shell中再次进行测试。
- Another common source of problems is the syntax in the cronjob. Remember that there are special characters you can use for lists (commas), to define ranges (dashes -), to define increment of ranges (slashes), etc. Take a look: http://www.softpanorama.org/Utilities/cron.shtml
- 另一个常见的问题来源是cronjob中的语法。请记住,有一些特殊的字符可以用于列表(逗号),定义范围(dashes -),定义范围的增量(斜杠),等等。
#5
2
On Ubuntu you can enable a cron.log
file to contain just the CRON entries.
在Ubuntu上你可以启用cron。日志文件只包含CRON条目。
Uncomment the line that mentions cron
in /etc/rsyslog.d/50-default.conf
file:
取消注释在/etc/rsyslog.d/50 default中提到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
Save and close the file and then restart the rsyslog
service:
保存并关闭文件,然后重新启动rsyslog服务:
sudo systemctl restart rsyslog
You can now see cron log entries in its own file:
您现在可以在它自己的文件中看到cron日志条目:
sudo tail -f /var/log/cron.log
Sample outputs:
示例输出:
Jul 18 07:05:01 machine-host-name CRON[13638]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
However, you will not see more information about what scripts were actually run inside /etc/cron.daily
or /etc/cron.hourly
, unless those scripts direct output to the cron.log (or perhaps to some other log file).
但是,您不会看到更多关于脚本在/etc/ cron中运行的信息。每日或/etc/cron.每小时,除非这些脚本直接输出到cron。日志(或其他日志文件)。
If you want to verify if a crontab is running and not have to search for it in cron.log
or syslog
, create a crontab that redirects output to a log file of your choice - something like:
如果您想验证crontab是否正在运行,而不需要在cron中搜索它。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
Steps taken from: https://www.cyberciti.biz/faq/howto-create-cron-log-file-to-log-crontab-logs-in-ubuntu-linux/
步骤:https://www.cyberciti.biz/faq/howto-create-cron-log-file-to-log-crontab-logs-in-ubuntu-linux/
#6
1
Incase you're running some command with sudo, it won't allow it. Sudo needs a tty.
如果你和sudo一起运行某个命令,它不会允许的。Sudo需要遥控。
#7
0
All of the above is not working for me. So, I have to add the 'MAILTO=[my email]' on the top of the crontab's file at /etc/cron.d/ and I got the answer because the my cron command got errors.
以上这些对我都不起作用。所以,我必须在crontab文件的顶部添加“MAILTO=[我的邮件]”。d/我得到了答案,因为我的cron命令有错误。
#8
0
cron
already sends the standard output and standard error of every job it runs by mail to the owner of the cron job.
cron已经将其运行的每个作业的标准输出和标准错误发送给cron作业的所有者。
You can use MAILTO=recipient
in the crontab
file to have the emails sent to a different account.
您可以在crontab文件中使用MAILTO=收件人将邮件发送到另一个帐户。
For this to work, you need to have mail working properly. Delivering to a local mailbox is usually not a problem (in fact, chances are ls -l "$MAIL"
will reveal that you have already been receiving some) but getting it off the box and out onto the internet requires the MTA (Postfix, Sendmail, what have you) to be properly configured to connect to the world.
为此,您需要让邮件正常工作。交付到本地邮箱通常不是一个问题(事实上,ls - l“邮件”美元会显示,你已经收到一些),但在互联网上得到了盒子,需要MTA(后缀,Sendmail,你)要正确配置连接到世界。
#1
228
* * * * * myjob.sh >> /var/log/myjob.log 2>&1
will log all output from the cron job to /var/log/myjob.log
将cron作业的所有输出记录到/var/log/myjob.log ?
You might use mail
to send emails. Most systems will send unhandled cron
job output by email to root or the corresponding user.
你可以用邮件发送电子邮件。大多数系统将通过电子邮件发送未处理的cron作业输出到根或对应的用户。
#2
46
By default cron logs to /var/log/syslog so you can see cron related entries by using:
默认情况下,cron日志到/var/log/syslog,以便您可以通过以下方式查看cron相关条目:
grep CRON /var/log/syslog
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
#3
8
Here is my code:
这是我的代码:
* * * * * your_script_fullpath >> your_log_path 2>&1
#4
8
There are at least three different types of logging:
至少有三种不同类型的日志记录:
-
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.
程序执行前的日志记录,只有当cronjob试图执行命令时才会记录日志。这个位于/var/log/syslog中,就像@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:
程序尝试执行后的错误记录,可以发送到电子邮件或文件,如@Spliffster所述。我喜欢登录到一个文件,因为有了电子邮件,你就有了一个新的问题来源,它检查邮件发送和接收是否正常工作。有时是,有时不是。例如,在一个简单的普通桌面机器中,您对配置smtp不感兴趣,有时您更喜欢将日志记录到文件中:
* * * * 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.
- 我还将考虑检查/ABSOLUTE_PATH_TO_LOG的权限,并从该用户的权限运行该命令。只是为了验证,当您测试它是否可能成为问题的潜在来源时。
- 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:
在cronjobs中有一些常见的问题来源:*要执行的二进制文件的绝对路径。当您从shell中运行它时,它可能会起作用,但是cron进程似乎使用了另一个环境,因此如果您不使用绝对路径,那么它并不总是能找到二进制文件。*二进制文件所使用的库。以前或多或少相同的点,但确保,如果简单地把命令的名称,指的是完全二进制使用完全相同的库,或者更好,检查如果您所指的是二进制的绝对路径是相同的你直接使用控制台时参考。可以使用locate命令找到二进制文件,例如:
$locate python
Be sure that the binary you will refer, is the very same the binary you are calling in your shell, or simply test again in your shell using the absolute path that you plan to put in the cronjob.
请确保您将引用的二进制文件与您在shell中调用的二进制文件是相同的,或者使用您计划放入cronjob的绝对路径在shell中再次进行测试。
- Another common source of problems is the syntax in the cronjob. Remember that there are special characters you can use for lists (commas), to define ranges (dashes -), to define increment of ranges (slashes), etc. Take a look: http://www.softpanorama.org/Utilities/cron.shtml
- 另一个常见的问题来源是cronjob中的语法。请记住,有一些特殊的字符可以用于列表(逗号),定义范围(dashes -),定义范围的增量(斜杠),等等。
#5
2
On Ubuntu you can enable a cron.log
file to contain just the CRON entries.
在Ubuntu上你可以启用cron。日志文件只包含CRON条目。
Uncomment the line that mentions cron
in /etc/rsyslog.d/50-default.conf
file:
取消注释在/etc/rsyslog.d/50 default中提到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
Save and close the file and then restart the rsyslog
service:
保存并关闭文件,然后重新启动rsyslog服务:
sudo systemctl restart rsyslog
You can now see cron log entries in its own file:
您现在可以在它自己的文件中看到cron日志条目:
sudo tail -f /var/log/cron.log
Sample outputs:
示例输出:
Jul 18 07:05:01 machine-host-name CRON[13638]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
However, you will not see more information about what scripts were actually run inside /etc/cron.daily
or /etc/cron.hourly
, unless those scripts direct output to the cron.log (or perhaps to some other log file).
但是,您不会看到更多关于脚本在/etc/ cron中运行的信息。每日或/etc/cron.每小时,除非这些脚本直接输出到cron。日志(或其他日志文件)。
If you want to verify if a crontab is running and not have to search for it in cron.log
or syslog
, create a crontab that redirects output to a log file of your choice - something like:
如果您想验证crontab是否正在运行,而不需要在cron中搜索它。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
Steps taken from: https://www.cyberciti.biz/faq/howto-create-cron-log-file-to-log-crontab-logs-in-ubuntu-linux/
步骤:https://www.cyberciti.biz/faq/howto-create-cron-log-file-to-log-crontab-logs-in-ubuntu-linux/
#6
1
Incase you're running some command with sudo, it won't allow it. Sudo needs a tty.
如果你和sudo一起运行某个命令,它不会允许的。Sudo需要遥控。
#7
0
All of the above is not working for me. So, I have to add the 'MAILTO=[my email]' on the top of the crontab's file at /etc/cron.d/ and I got the answer because the my cron command got errors.
以上这些对我都不起作用。所以,我必须在crontab文件的顶部添加“MAILTO=[我的邮件]”。d/我得到了答案,因为我的cron命令有错误。
#8
0
cron
already sends the standard output and standard error of every job it runs by mail to the owner of the cron job.
cron已经将其运行的每个作业的标准输出和标准错误发送给cron作业的所有者。
You can use MAILTO=recipient
in the crontab
file to have the emails sent to a different account.
您可以在crontab文件中使用MAILTO=收件人将邮件发送到另一个帐户。
For this to work, you need to have mail working properly. Delivering to a local mailbox is usually not a problem (in fact, chances are ls -l "$MAIL"
will reveal that you have already been receiving some) but getting it off the box and out onto the internet requires the MTA (Postfix, Sendmail, what have you) to be properly configured to connect to the world.
为此,您需要让邮件正常工作。交付到本地邮箱通常不是一个问题(事实上,ls - l“邮件”美元会显示,你已经收到一些),但在互联网上得到了盒子,需要MTA(后缀,Sendmail,你)要正确配置连接到世界。