Crontab及其日志

时间:2015-06-04 11:35:57

标签: crontab sysadmin

我的第一次写crontab。

以下是时间和日期的结构

 # * * * * *  command to execute
 # │ │ │ │ │
 # │ │ │ │ │
 # │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
 # │ │ │ └────────── month (1 - 12)
 # │ │ └─────────────── day of month (1 - 31)
 # │ └──────────────────── hour (0 - 23)
 # └───────────────────────── min (0 - 59)

我把条目放在下面

00 06     * /bin/sh /opt/cleanup.sh

认为这不用担心。

哪里可以看到crontab的任何日志?

2 个答案:

答案 0 :(得分:1)

您需要使用所有参数。所以每天早上6点运行使用:

00 06 * * * /bin/sh /opt/cleanup.sh

您可以在/var/log/cron中查看日志。

有关详细信息,您可以查看精美的部分Debugging crontab in [crontab] tag wiki

答案 1 :(得分:1)

通常cron命令的输出通过邮件(man mail)发送给所有者,但是当你执行返回输出的代码(stdout和stderr)时会这样做。登录时,您应该看到“有新邮件”之类的内容。我不知道如果你的错误cron时间表(参见@ fedorqui的回复)会抛出一个错误日志。在任何情况下,要将已安排的cron作业的输出和错误存储在文件而不是邮件上,您可以像这样重定向输出:

00 06 * * * /bin/sh /opt/cleanup.sh > /where/you/have/write/permission/cleanup.log 2>&1

如果你想追加而不是覆盖,只需使用两个>,如下所示:

00 06 * * * /bin/sh /opt/cleanup.sh >> /where/you/have/write/permission/cleanup.log 2>&1

要禁用日志,只需安排以下内容:

00 06 * * * /bin/sh /opt/cleanup.sh > /dev/null 2>&1

2>&1表示“将通道2(错误)重定向到通道1的指针(标准输出)”。标准输出被重定向到文件(/my/file.sh > /my/file.log)。