How to run a command in the background and notify me by email when it's done

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

As for the cherry on top, should any errors it encounters be written to a text file when an error occurs?

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

How can I do this with my team?

+4
source share
1 answer
 yourcommand 2>&1 | mail -s "yourcommand is done" yourname@example.com 

Bit 2>&1 says: "Make my mistakes look like regular output," and the rest say: "Take the usual output and send it to me with a good storyline."

Note that this does not work in the csh shell family, only Bourne ( sh , bash , ksh ...), AFAIK. So run #!/bin/sh or #!/bin/bash . (NB You can pass both descriptors to csh using yourcommand |& mail ... , but only a crazy yourcommand |& mail ... writes scripts using csh .)

UPDATE:

How can I write errors to a log file and then just send my completion by email?

if you mean just emailing the fact that this is done,

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

if you mean just send error messages (in this case you do not need a log file) ( fixed as @Trey ),

 yourcommand 2&>1 1>/dev/null | mail -s "yourcommand is done" yourname@example.com 
+9
source

Source: https://habr.com/ru/post/1311681/


All Articles