简体   繁体   English

如何在后台运行命令并在完成后通过电子邮件通知我

[英]How to run command in the background and notify me via email when done

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? 对于顶部的cherry,它遇到的任何错误都应该在发生错误时写入文本文件?

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

How can I do this with my above command? 我怎么能用上面的命令做到这一点?

yourcommand 2>&1 | mail -s "yourcommand is done" yourname@example.com

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" 2>&1位表示“让我的错误像常规输出一样”,其余的则说“采取常规输出并将其邮寄给我一个不错的主题行”

Note that it doesn't work in csh family of shells, only Bourne ( sh , bash , ksh ...), AFAIK. 请注意,它不适用于csh系列shell,只能使用Bourne( shbashksh ...),AFAIK。 Thus, run under #!/bin/sh or #!/bin/bash . 因此,在#!/bin/sh#!/bin/bash (NB You can pipe both descriptors in csh using yourcommand |& mail ... , but only a madman writes scripts using csh .) (注意:您可以使用yourcommand |& mail ...csh管道两个描述符,但只有疯子才能使用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), 如果你的意思是只是通过电子邮件发送错误(在这种情况下你不需要日志文件)( 修复为@Trey说),

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM