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

Abs*_*Abs 4 linux bash shell

我有以下命令,需要很长时间才能运行(几个小时).我想将它作为后台进程,并在完成后向我发送电子邮件.

对于顶部的cherry,它遇到的任何错误都应该在发生错误时写入文本文件?

find . -type f -name "*.rm" -exec ./rm2mp3.sh \{} \; -exec rm \{} \;
Run Code Online (Sandbox Code Playgroud)

我怎么能用上面的命令做到这一点?

Ama*_*dan 10

yourcommand 2>&1 | mail -s "yourcommand is done" yourname@example.com
Run Code Online (Sandbox Code Playgroud)

2>&1位说"让我的错误像常规输出一样",其余的说"采取常规输出并将其邮寄给我一个很好的主题行"

需要注意的是它不工作的csh家庭贝壳,只有伯恩(sh,bash,ksh...),AFAIK.因此,在#!/bin/sh或下运行#!/bin/bash.(注意:您可以在csh使用中管道两个描述符yourcommand |& mail ...,但只有疯子才能使用csh.)

更新:

我如何将错误写入日志文件,然后在完成后发送电子邮件给我自己?

如果你的意思是通过电子邮件发送它已经完成的事实,

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

如果你的意思是只是通过电子邮件发送错误(在这种情况下你不需要日志文件)(修复为@Trey说),

yourcommand 2&>1 1>/dev/null | mail -s "yourcommand is done" yourname@example.com
Run Code Online (Sandbox Code Playgroud)

  • 我认为重定向实际上应该在最后一个命令上交换,以将stderr重定向到stdout并关闭stdout:`yourcommand 2>&1 1>/dev/null` (2认同)