这个 cronjob 在 cron 中失败但在命令行上却不会失败,这有什么原因吗?

这个 cronjob 在 cron 中失败但在命令行上却不会失败,这有什么原因吗?

我编写了一个小程序,当文件列表发生变化时它会通过电子邮件发给我 - 我使用 sha512 生成哈希列表,然后定期检查这些哈希是否仍然匹配。

*/5     *   *   *   *   /usr/bin/sha512sum --status -c /sha512.sumlist && echo "Success" > /dev/null || echo "Check robots.txt and index.html in /var/www as staging sites are now potentially exposed to the world and the damned googlebot" | /usr/bin/mail -s "Default staging server files have changed" [email protected]

它在命令行上运行良好:

/usr/bin/sha512sum --status -c /sha512.sumlist && echo "Success" > /dev/null || echo "Check robots.txt and index.html in /var/www as staging sites are now potentially exposed to the world and the damned googlebot" | /usr/bin/mail -s "Default staging server files have changed" [email protected]

当我将其作为 cronjob 运行时,它每次运行时都会发送电子邮件并显示失败消息,而不仅仅是在 sha512sum 检查失败时。

我是不是因为太匆忙而错过了什么愚蠢的事情?

我忘了说我正在运行一台 Ubuntu 机器。

答案1

在缩减一行并删除通知方面之后,我发现我需要在运行命令之前移动到正确的目录sha512sum

现在看起来像这样:

*/5     *   *   *   *   cd /var/www; /usr/bin/sha512sum --status -c /sha512.sumlist && echo "Success" > /dev/null || echo "Check robots.txt and index.html in /var/www as staging sites are now potentially exposed to the world and the damned googlebot" | /usr/bin/mail -s "Default staging server files have changed" [email protected]

中的文件路径sha512.sumlist是相对的,因此需要在包含要检查的文件的文件夹中运行该命令。在我的例子中,这只是/var/www

答案2

我认为您忘记指定用户,例如root

*/5     *   *   *   *   root    /usr/bin/sha512sum --status -c /sha512.sumlist && echo "Success" > /dev/null || echo "Check robots.txt and index.html in /var/www as staging sites are now potentially exposed to the world and the damned googlebot" | /usr/bin/mail -s "Default staging server files have changed" [email protected]

相关内容