-   Linux - Newbie (
-   -   output of a tar command not sending to my E-Mail (

amario26 05-05-2011 05:45 AM

output of a tar command not sending to my E-Mail

Basically I have a script that runs every day (via crontab) and it works perfectly! Only problem is that the job will only send the output of most of the stuff and not the tar bit..

This is the main part of my script:


if (( $days1 <= 5 ))
                echo "*******" "Begin Removing All Previous Log Files" "*******"
                        rm ./*cron.log
                echo "*******" "Begin Removing Files" "*******"
                        ssh username@servername rm ./../test/$TODAY/*.gz
                echo "*******" "Checking the following directories: $BACKUP" "*******"
                set -- $BACKUP
                        for i in "$@"
                                if [ -d $i ]; then
                                echo "Directory $i exists"
                                echo "!!!!!!! Directory $i doesnt exist - This directory will not be backed up. Your backup will continue as normal, skipping this directory. !!!!!!!"
                echo "*******" "Begin compressing and transferring files" "*******"
                        tar -czp $BACKUP | ssh -c blowfish username@servername "dd of=/home/test/$TODAY/"$DATETIME"__TestBackup.tar.gz"
                echo "Your backup has failed"

It will only email me up to the bit where it says echo "*******" "Begin compressing and transferring files" "*******" after that it wont email me the output of the tar command just below it...

Here is my crontab line that does the mailing:


00 22 * * 1-5 bash /home/user/ | mail -s "Backup Log"
Do any of you experts know why that is? I've been trying for ages to figure it out! I cant even get mutt -a to work.. nor tee..

Many thanks for your help in advance, its appreciated.

neonsignal 05-05-2011 06:20 AM

The standard output stream of tar is the tarfile itself (which is put by the script into the backup file). I'm guessing that what you want to see is the standard error stream, where diagnostics are more often sent.

Since your crontab script only pipes the stdout to the email, you are losing the stderr output. In bash you can use something like this to pass both stderr and stdout through the pipe to the email:


00 22 * * 1-5 bash /home/user/ 2>&1 | mail -s "Backup Log"
(where the '2>&1' says to redirect stderr output to stdout)

Alternatively you can just use:


00 22 * * 1-5 bash /home/user/ |& mail -s "Backup Log"
(which might be more portable, in cases where you aren't using bash)

amario26 05-05-2011 07:03 AM

Thanks neonsignal!

That was pretty much what I wanted but the only thing is now that when the E-Mail arrives it tells me its failed.. It was working before I added '2>&1' to the crontab as you suggested.. any ideas?

Many thanks

amario26 05-05-2011 07:09 AM

Actually.. ignore me.. I was typing some stuff and my fat fingers made a typo!

Thanks dude!

All times are GMT -5. The time now is 02:49 PM.