stdout and stderr of script doesn't get redirected when executed by cron

3

./script &>> log.txt

I get a nice logfile, but if I have the same command executed by cron, lets say the crontab looks like this:

* * * * * '/home/user/script &>> /home/user/log.txt'

the log.txt will just be empty, I tried " and ' and ` and no ticks to enclose the command, any idea why the streams won't get written into the file?

gletscher

Posted 2010-03-28T21:17:32.997

Reputation: 363

Answers

6

Chances are that the shell that cron uses doesn't support the shorthand redirection operator &>> that Bash supports.

You should use the portable form which is supported in the Bourne shell and others:

* * * * * /home/user/script >> /home/user/log.txt 2>&1

That says "append the standard output (file descriptor 1) to the file and send standard error (file descriptor 2) to the same place".

And you don't need any quotes.

Paused until further notice.

Posted 2010-03-28T21:17:32.997

Reputation: 86 075

+1 for also redirect the std error! No wonder it's outputting nothing. – User – 2016-07-20T23:04:46.990

1

  1. Don't include any quotes for the command in the crontab.

  2. Consider running an environment setting script that does its own redirection, instead of relying on cron to do it.

In my experience, the less there is in the crontab file, the better. My crontab files consist of the time controls plus a simple absolute command name (ksh) and the command it is to run:

#   @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
#   Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min     Hour    Day     Month   Weekday Command
#-----------------------------------------------------------------------------
0        *       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/hourly
1        1       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23       1       *       *       1-5     /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2        3       *       *       0       /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21       3       1       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/monthly

Jonathan Leffler

Posted 2010-03-28T21:17:32.997

Reputation: 4 526

1Your point #1 is a bit misleading (I guess you mean "Do not quote the whole command."). cron usually uses /bin/sh which is certainly capable of handling quoting, but the OP's quotes were used incorrectly (likewise, the redirection was not sh compatible). Some versions of cron will let crontab files specify which shell should interpret the commands, so it might be possible to use the features and syntax of your favorite shell right in a crontab entry. Though certainly I agree that keeping crontab entries simple is nice suggestion. – Chris Johnsen – 2010-03-28T22:31:16.450

@Chris: yes - my statement is too sweeping and your analysis is correct. Thanks. – Jonathan Leffler – 2010-03-29T06:29:28.980