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

命令在终端中运行正常,但在作为cron作业运行时运行不正常,cron,cron-task,crontrigger,Cron,Cron Task,Crontrigger,我正在尝试使用cron每分钟运行以下测试命令: apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1 这将运行apachectl status,如果输出包含“当前正在处理的请求”,它将把命令date的结果打印到文件output.txt中 如果我以root用户的身份在终端中运行这个,那么就没有问题了。但是,每分钟将其作为cron

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

apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1
这将运行
apachectl status
,如果输出包含“当前正在处理的请求”,它将把命令
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

也许cron行最好是
***/usr/sbin/apachectl status…..
@RomeoNinov同意,但这不是应该解决的问题=)