183

I have a backup script which I need to run at a particular time of a day so I am using cron for this task and from within cron am also trying to redirect the output of backup script to a logfile.

crontab -e

*/1 * * * * /home/ranveer/backup.sh &>> /home/ranveer/backup.log

In the above cron entry I am redirecting both stderr and stdout to a log file.

The above cron job executes fine according to syslog and it performs the task mentioned in the backup.sh file but it doesn't write anything to the log file.

/var/log/syslog

Oct 19 20:26:01 ranveer CRON[15214]: (ranveer) CMD (/home/ranveer/backup.sh &>> /home/ranveer/backup.log)

When I run the script from cli it works as required and output is written to a log file

ranveer@ranveer:~$ ./backup.sh &>> backup.log 
ranveer@ranveer:~$ cat backup.log
Fri Oct 19 20:28:01 IST 2012
successfully copied testdir
test.txt successfully copied
-------------------------------------------------------------------------------------
ranveer@ranveer:~$ 

So, why the output of file is not getting redirected to the file from within cron.

1

3 Answers 3

282

I solved the problem. There are two ways:

M1

Change the redirection from &>> to 2>&1. So now crontab -e looks like

*/1 * * * * /home/ranveer/vimbackup.sh >> /home/ranveer/vimbackup.log 2>&1

I believe the above works because by default cron is using sh to run the task instead of bash so &>> is not supported by sh.

M2

Change the default shell by adding SHELL=/bin/bash in the crontab -e file.

6
  • 23
    cron is like sh: it doesn't pick out a single program. There are many implementations. The most prevalent implementation is Vixie cron. I am the (current, not the original) author of another. I believe that most cron daemons will use the system's sh, but that can vary in whether it accepts &>>. Some cron daemons (like mine) don't enable you to change which shell executes the cron lines with SHELL=... lines in the crontab. I'm glad you found a solution that works for you; just thought it was worth pointing out there are many variables that could affect whether it works for others.
    – dubiousjim
    Commented Oct 19, 2012 at 15:50
  • 1
    How to insert YYYY-MM-DD_hh-mm-sec into output file name, so that every file name is different and kept without rewriting?
    – Danijel
    Commented Jan 11, 2016 at 13:18
  • 2
    @Danijel : This should help serverfault.com/questions/117360/…
    – RanRag
    Commented Jan 15, 2016 at 9:09
  • 2
    */1 * * * * /home/ranveer/vimbackup.sh &>> /home/ranveer/vimbackup.log does the same thing too. Commented Aug 15, 2017 at 14:56
  • 1
    @adnanmuttaleb > replaces the contents of the file with the STDOUT from the command, and >> appends the file with the output from the command.
    – 111---
    Commented Jul 7, 2021 at 12:29
11

disclaimer [1].

I would like to add a footnote or addendum to @RanRag's answer.

Make sure your shell redirection syntax conforms to /bin/sh. If you try to use shell redirection syntax that is not valid with /bin/sh then your command will fail and your cron job will not ever run.

In your /etc/cron.d/example1 config files if you specify a user other than root and that user's login shell is not /bin/bash... you must still use /bin/sh syntax in /etc/cron.d/example1 command.


For example

If your user has shell csh or zsh or ksh set for his login shell. In your /etc/cron.d/example1 config file, the command must use /bin/sh syntax. Specifically any shell redirection must be /bin/sh syntax.

If you try to use for example csh shell redirect syntax in your /etc/cron.d/example1, then your cron job will never run. The logfile for crond located at /var/log/cron shall say that the command is run but the command will error out with a syntax error before your command gets run.

Where does crond emit error messages for a syntax error?

The error is not ever reported in /var/log/cron. crond instead by default emits any error messages using mail. So you must check /var/spool/mail/${USER} to see what is the error.

[1]

Disclaimer

  • This answer assumes a sysv system
  • systemd information may differ
  • Specifically this information was learned for centos-6 distro and may not apply to different sysv distros
    • I mention centos-6 specifically, because different distros may have a different crond implementation that differs from centos-6
1
  • 2
    if you wish to have your /etc/cron.d/example1 command use a different shell, you can use set the SHELL= in your /etc/cron.d/example1 config file. Commented May 29, 2017 at 20:08
3

You can direct output information to a log file with 2 greater-than signs like this example:

* * * * * /usr/bin/python /path/to/script.py >> /path/to/logfile/output.log

Please see this convient crontab-line generator in this link: https://crontab-generator.org/

0

You must log in to answer this question.

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