Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
  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
share|improve this answer
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. –  Chris Johnsen Mar 28 '10 at 22:31
    
thanks alot for the insight –  gletscher Mar 29 '10 at 0:11
    
@Chris: yes - my statement is too sweeping and your analysis is correct. Thanks. –  Jonathan Leffler Mar 29 '10 at 6:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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