views:

66

answers:

1

Hello all,

I have the following command which will take ages to run (couple of hours). I would like to make it a background process and for it to send me an email when its done.

For the cherry on the top, any errors it encountered should write to a text file when an error has occurred?

find . -type f -name "*.rm" -exec ./rm2mp3.sh \{} \; -exec rm \{} \;

How can I do this with my above command?

+4  A: 
yourcommand 2>&1 | mail -s "yourcommand is done" [email protected]

The 2>&1 bit says "make my errors like the regular output", and the rest says "take the regular output and mail it to me with a nice subject line"

Note that it doesn't work in csh family of shells, only Bourne (sh, bash, ksh...), AFAIK. Thus, run under #!/bin/sh or #!/bin/bash. (N.B. You can pipe both descriptors in csh using yourcommand |& mail ..., but only a madman writes scripts using csh.)

UPDATE:

How would I write the errors to a log file and then just email my self on completion?

if you mean just email the fact that it is done,

yourcommand 1>/dev/null 2>mylogfile ; (echo "done!" | mail -s "yourcommand is done")

if you mean just email the errors (in which case you don't need the log file) (fixed as @Trey said),

yourcommand 2&>1 1>/dev/null | mail -s "yourcommand is done" [email protected]
Amadan
The problem with this is that regular output will also come through and not just errors, right?
Abs
How would I write the errors to a log file and then just email my self on completion?
Abs
I updated my answer based on your additional questions.
Amadan
Abs
Trey
Dennis Williamson
@Dennis. I try to just close my shell (SSH connection using Putty) when I run the command. But it runs the job in the background and finishes but it doesn't send the email when I tried the `nohup` and `$`.
Abs
@Trey: Gah, true! Thanks - I fixed it
Amadan
Dennis Williamson