Search code examples
croncron-taskcrontrigger

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


I am trying to run the following test command every minute with cron:

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

This would run apachectl status and if the output contains 'requests currently being processed', it would print the result of the command date to the file output.txt.

If I run this in the terminal as the root user, there is no issue. However, running it as a cronjob every minute (I've added it to /var/spool/cron/root) as follows:

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

Does nothing - the output.txt file never gets generated and the job never shows in the log at /var/log/cron. What might the issue be?


Solution

  • You should specify the full path to the executable files, you may easily find it using which command, like which apachectl etc.

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