Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to email Bash script results to user

Status
Not open for further replies.

rosslaird

Technical User
Joined
May 10, 2004
Messages
6
Location
CA
I've been looking around for awhile trying to figure out how to do what seems like a very simple thing:

I have a Bash script that runs rsync and fmirror for backups. It's only a few lines long (it just has the commands to run the backup). When I run script in a console window, I see it working away, and I see any errors it encounters. I want to email myself this output. I've tried various forms of "mail ..." in the script, but nothing I've tried works so far. I think I just don't know enough.

Any suggestions?

Thanks.
 
You should use the bash redirection features for this. for example , redirecting ls to a file...

Code:
lullysing@server:~> ls -l 1>results
lullysing@server:~> cat results
total 1696
drwx------    3 lullysing users         512 2004-05-04 00:18 Desktop
drwxr-xr-x    4 lullysing users         496 2004-04-11 16:31 Documents
drwx------    2 lullysing users          48 2003-08-08 14:00 Mail
drwxr-xr-x    3 lullysing users         320 2003-07-29 23:02 OpenOffice.org1.0.1
-rw-r--r--    1 lullysing users         202 2004-04-26 16:47 cv.txt

here's another example :

Code:
lullysing@server:~> mount /no/such/filesystem 1>results 2>error
lullysing@server:~> cat results
lullysing@server:~> cat error
mount: can't find /no/such/filesystem in /etc/fstab or /etc/mtab

In which case the command results were in "results"and errors were in the file "error".

You dig?



_____________________________
when someone asks for your username and password, and much *clickely clickely* is happening in the background, know enough that you should be worried.
 
try
cat file | bin/mail -s "SUBJECT user@domain.com

>---------------------------------------Lawrence Feldman
SR. QA. Engineer SNAP Appliance
lfeldman@snapappliance.com

 
The above is close to what I had originally (ls -l). The problem is that this sends a listing of files, but does not show other messages that I see onscreen when I run the script from a console. From the console, I see rsync's and fmirror's own messages (errors and other information), which I would like to redirect into the email.

 
rsync parms 2>&1 > filename.txt;
cat filename.txt | /bin/mail -s "SUBJECT" user@domain.com;

Any output that deviates from the redirection above is the product of a poorly written program and should be brought to the author's attention.

Surfinbox.com Business Internet Services - National Dialup, DSL, T-1 and more.
 
The redirection part works fine: I get the email of the text file. It's the first part that's the problem. I'm not sure where in the script to put "rsync parms 2>&1 > filename.txt;" when I enter than line into a console window I get an error message from rsync (which makes sense, since "parms" is not an rsync option).

Maybe the best thing it to actually show the script:

fmirror -Rf fmirror.conf
rsync --delete -varuzP /usr/local/ /home/rosslaird/sitecopy --update office
rsync -e ssh -varuzP /home/rosslaird/ root@laird:/home/rosslaird
rsync -e ssh -varuzP /home/rosslaird/Documents/ root@laird:/disks/xandros/home/rosslaird
ls -l 1>backup.txt
mail -s "Backup Completed" ross@rosslaird.com < backup.txt

Just to be clear: I'd like the text file to have the screen output in it, in other words what I see when I run the script from a console.
 
parms" was an example, sorry if you took it literally.

put the first line of your script through the last line before "ls -| 1>backup.txt" into a single script file (I think you did this. Call this file "bak.sh" (or anything else you want).

then run it from command line or crontab entry by
"./bak.sh 2>&1 1> output.txt; cat output.txt | \
mail -s "Backup Completed" ross@rosslaird.com"

The use of the "\" character at the end of lines on a bash command is a way of continuing to another line for readability without effectively hitting [ENTER] and submitting the command.

Should work

Surfinbox.com Business Internet Services - National Dialup, DSL, T-1 and more.
 
Thanks for the help -- but nope, it still doesn't work.

First, I tried the line exactly as you specified it above. No dice. Then I broke it down, removing bits off the end until it worked.

I get this far: "./bak.sh", but anything after this that I put on the line causes the script to halt after fmirror runs. Even the numbers. It just takes the basic "run script" command only, no extras. If I just run ./bak.sh from the command line, it works fine (of course).

What gives?

 
Try this:
Code:
( fmirror -Rf fmirror.conf
 [script abbreviated for succinctness...]
ls -l ) 2>&1 | mail -s "Backup Completed" ross@rosslaird.com

There's no reason to use a temp file, though if you do try this:

Code:
tempfile=/tmp/whatever

trap "rm $tempfile" EXIT
do whatever > $tempfile

This will remove the tempfile on a successful exit
 
Thanks for the tip, Eric --

but no, it doesn't work. When I put the parentheses into the script, which is called remote_backup.sh, and I run:

./remote_backup.sh

from the command line, it just hangs.
 
That's very strange.

The only thing I can think of is that one of your utilities, fmirror or sitecopy, is written in such a way that it assumes stdout or stderr is on terminal and hangs when it's redirected. I'm not familiar with either of them, but if they have a curses interface or try to do clever things with their output like redrawing the screen to draw a progress bar, that could be the problem.

Try starting with:

Code:
( ls -l ) 2>&1 | mail -s "Backup Completed" ross@rosslaird.com

then add each directive one by one until you find the component that is causing the script to hang.
 

Thanks for all the help. Finally, I have this working -- I don't know why it works when the other things I tried did not, but at this point I'm just happy that it does. Here's what I did:

1. I created a cron job that looks like this:

0 12 *** /home/myhome/backup.txt

2. In file backup.txt, I listed the commands, using only rsync (I could have used fmirror also, but using one command for the whole thing was simpler).

3. In my home directory, I created a file called .forward, with a single line that has my email address. (Note the dot.) This allows my local mailer to send a message to my remote account (what my mail program checks).

Now, cron runs the backup and emails me exactly the right output (i.e what I see onscreen when I run it from a console).

This seems like an odd workaround, and I sort of stumbled upon the solution, but it works perfectly.

Cheers.

Ross
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top