2

I'm trying to redirect STDOUT and STDERR with &>> in my cron job, but I'm not seeing anything in the log file. The file is being created on the first run, but no OUT or ERR.

When I run a command with the &>> redirect directly, it works as expected.

I can successfully redirect output with ... >> /tmp/log 2>&1, but according to this GNU page (3.6.5), these two approaches are equivalent, so I'm not sure why the first approach isn't working.

Any ideas what I'm missing here?

Code sample:

#
# test.bash
#
echo "OUT"
ecko "ERR"
#
# crontab
#
* * * * * /bin/bash /root/test.bash &>> /tmp/test_log_1      # NOT WORKING
* * * * * /bin/bash /root/test.bash >> /tmp/test_log_2 2>&1  # WORKING
#
# using redirect directly works as expected
#
$ /bin/bash /root/test.bash &>> /tmp/test_log_3
$ /bin/bash /root/test.bash &>> /tmp/test_log_3
$ cat /tmp/test_log_3
OUT
/root/test.bash: line 2: ecko: command not found
OUT
/root/test.bash: line 2: ecko: command not found

System details:

  • Ubuntu 20.04.4 LTS
  • Bash 5.0.17
  • Cron 3.0pl1-136ubuntu1

1 Answer 1

4

Cron interprets job entries using /bin/sh by default, which doesn't support the &>> redirection syntax.

If you want the job to be run in a different shell, you can set the SHELL variable inside your crontab:

SHELL=/bin/bash
* * * * * /bin/bash /root/test.bash &>> /tmp/test_log_1

Please refer to man 5 crontab for details.

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.