I try to write the message "Ran Cronjob XY" to a logfile if my cronjob ran.


/opt/cpanel/ea-php73/root/usr/bin/php /home/company/example.de/bin/magento list 2>&1 | grep -v "Ran Cronjob XY" >> /home/company/example.de/var/log/test.cron.log

But this fails and logs the output of the command /opt/cpanel/ea-php73/root/usr/bin/php /home/company/example.de/bin/magento list instead of just Ran Cronjob XY


Show how to print "ran successfully" if the command was executed successfully and "failed" if not.

if some_command >/dev/null 2>&1; then
    echo ran successfully
    echo failed
fi >>logfile

The above code will run the command some_command, discard its output, and then append the text ran successfully to the file logfile if the command finished successfully. If the command fails, it would append the text failed to logfile.

In your case, for simplicity (since you have so long pathnames in your commands), I would put this in its own wrapper script and execute that script with a cron job.

The script would look like




if php /home/company/example.de/bin/magento list >/dev/null 2>&1
    echo ran successfully
    echo failed
fi >>"$logfile"

I modify PATH in the script to allow running php without an absolute path.

That script would then be scheduled:

* * * * * /path/to/thescript.sh

... where the * * * * * should be replaced by the actual schedule.

If you'd wanted to turn this into a "one-liner" for use directly in a crontab entry:

* * * * * if /opt/cpanel/ea-php73/root/usr/bin/php /home/company/example.de/bin/magento list >/dev/null 2>&1; then echo ran successfully; else echo failed; fi >>/home/company/example.de/var/log/test.cron.log

... where the * * * * * should be replaced by the actual schedule.

  • Might be simpler to just write as * * * * * (/opt/cpanel/ea-php73/root/usr/bin/php ... && echo "worked" || echo "failed") > logfile.
    – terdon
    Jun 18 '20 at 16:53
  • @terdon Should we replace ... with /home/company/example.de/bin/magento list or with /home/company/example.de/bin/magento list >/dev/null 2>&1 in your example?
    – Black
    Jun 19 '20 at 8:36
  • 1
    @Black I was thinking of this: * * * * * ( /opt/cpanel/ea-php73/root/usr/bin/php /home/company/example.de/bin/magento list >/dev/null 2>&1 && echo ran successfully || echo failed ) >>/home/company/example.de/var/log/test.cron.log. But note that this is just a question of syntax, that does exactly the same thing as Kusalananda's version, and his version is clearer and easier to understand. Mine is just a little bit shorter.
    – terdon
    Jun 19 '20 at 9:20
  • @terdon Yours would work, but lists with && and || doesn't follow the same clear semantics as the slightly more verbose if ...; then ...; else ...; fi, and it's easy to get it wrong when trying to generalise it. The two are simply not equivalent. For example, echo failed would run if either of the preceding two commands failed.
    – Kusalananda
    Jun 19 '20 at 9:46
  • @Kusalananda you mean if the echo "ran successfully" failed? True, unlikely, but yes very true.
    – terdon
    Jun 19 '20 at 9:51

You seem to want to echo a message to the log file if the first command ran successfully. In that case, you can use a double ampersand && instead of a pipe |: /opt/cpanel/ea-php73/root/usr/bin/php /home/company/example.de/bin/magento list 2>&1 && echo "Ran Cronjob XY" >> /home/company/example.de/var/log/test.cron.log

  • 3
    please use >/dev/null 2>&1 instead of just 2>&1 in your answer because the command output is not needed.
    – binarysta
    Jun 18 '20 at 11:01
  • This will just call the first command and then the next, independently if the first command run successfully or not. So how is this a difference from my version?
    – Black
    Jun 18 '20 at 11:06
  • 3
    No @Black, it will not executed independently. && is logical AND. the second command will executed if the first is successful. have a look at stackoverflow.com/a/30508672/5217119.
    – binarysta
    Jun 18 '20 at 11:12
  • @summertime, and how can I log if the command was not running successfull?
    – Black
    Jun 18 '20 at 12:06
  • 1
    @Black You can't affirm your cron job ran your way (the '| echo' way) : put anything that doesn't exist before your pipe, nothing will run, but the echo will write. Test it with "thisisnotacommand | echo "Ran Cronjob XY" >> /home/company/example.de/var/log/test.cron.log"
    – user4089
    Jun 18 '20 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.