命令 运行 在终端中正常,但当 运行 作为 cron 作业时则不然

Command runs fine in terminal, but not when run as cron job

我正在尝试使用 cron 每分钟 运行 以下测试命令:

apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1

这将 运行 apachectl status,如果输出包含 'requests currently being processed',它会将命令 date 的结果打印到文件 output.txt。

如果我 运行 在终端中以 root 用户身份执行此操作,则没有问题。但是,运行每分钟将其作为 cronjob(我已将其添加到 /var/spool/cron/root),如下所示:

*/1 * * * * apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1

什么都不做 - output.txt 文件永远不会生成,作业也永远不会显示在 /var/log/cron 的日志中。可能是什么问题?

您应该指定可执行文件的完整路径,您可以使用 which 命令轻松找到它,例如 which apachectl

*/1 * * * * /usr/sbin/apachectl status | /bin/grep 'requests currently being processed' && 'date' >> /output.txt 2>&1