[CentOS] Runing a script under cron. Append stdout and stderr to a log file, propagate stderr back to cron for email reporting

Alexander Georgiev alexander.georgiev at gmail.com
Sat Sep 22 14:11:01 UTC 2007


Dear List,

I want to run a rsync-ing script in cron, generating a very verbose
-vv rsync log in a log file. The log file should combine both stderr
and stdin, which is easy:

     backup.sh >>/var/log/backup.log 2>&1

However, I would like to propagate only stderr to cron - in case there
has been an error, cron will mail me the assembled stderr output.

I tried the following:

$ (./backup.sh > /var/log/backup.log) 2>&1 | tee -a /var/log/backup.log

which propagates the stderr to cron, but lines written to
/var/log/backup.log are out of sequence - for example if the contents
of backup.sh are:

#!/bin/bash

echo "out 1" >&1
echo "out 2" >&1
echo "out 3" >&1
echo "out 4" >&1
echo "err 5" >&2
echo "out 6" >&1
echo "out 7" >&1
echo "out 8" >&1

I get in /var/log/backup.log:

out 1
out 2
out 3
out 4
out 6
out 7
out 8
err 5

I would like the lines in their initial sequence.

Do you know a shell trick or a tool that I can use to achive desired goal?

Best regards,
alex



More information about the CentOS mailing list