[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Email output + logging too?
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Email output + logging too? |
Date: |
Mon, 16 Sep 2013 07:53:04 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Sat, Sep 14, 2013 at 09:15:58AM +0100, Sharon Kimble wrote:
> I have this script which uses obnam to back up my /home/$USER
> directory, and it outputs to 2 logs, and should also output to an
> email.
> =======================================
> LOGFILE="/home/boudiccas/logs/obnam.txt"
> LOGGING2="/home/boudiccas/cron/du.txt"
> ####################################################
> notify-send "Starting main backup..."
>
> obnam backup
>
> if [ "$?" -ne 0 ]; then
> notify-send "Unable to finish main backup."
> exit 1
> else
> notify-send "Finished main backup."
>
> du -sh /media/boudiccas/backup/obnam-home>>$LOGGING2 2>&1
>
> echo 'Sending Backup report : Backup of obnam-home completed', $(date
> -R) 'logged to'>>$LOGFILE 2>&1
>
> fi
> ==============================================
> As shown it just outputs to the logs but not the email.
Nothing in this script explicitly invokes a mail user agent. If this
is being run from a crontab, then the script's standard output and
standard error will automatically be emailed to someone (typically the
owner of the crontab).
So, I believe that your question really boils down to "How do I cause
all of my script's output to be sent to a logfile *and* also to stdout?"
Matthew's suggestion to put "tee -a" on every line that generates output
is one solution to this. However, it's tedious and inefficient.
Another solution is to set up a single redirection at the start of the
script:
#!/bin/bash
# Log all stdout+stderr
exec > >(tee -a /home/boudiccas/logs/obnam.txt) 2>&1
# The rest of your script goes here.
This is covered in more detail at http://mywiki.wooledge.org/BashFAQ/106