4

./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?

2 Answers 2

7

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.

1
  • +1 for also redirect the std error! No wonder it's outputting nothing.
    – User
    Commented Jul 20, 2016 at 23:04
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
2
  • 1
    Your 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. Commented Mar 28, 2010 at 22:31
  • @Chris: yes - my statement is too sweeping and your analysis is correct. Thanks. Commented Mar 29, 2010 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .