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

标签 linux bash shell

我有以下命令需要很长时间才能运行(几个小时)。我想让它成为一个后台进程,并让它在完成后给我发一封电子邮件。

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

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

如何使用上述命令执行此操作?

最佳答案

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

2>&1 位表示“让我的错误像常规输出一样”,其余部分表示“获取常规输出并用漂亮的主题行邮寄给我”

请注意,它在 csh 系列 shell 中不起作用,仅在 Bourne(shbashksh ...),据我所知。因此,在 #!/bin/sh#!/bin/bash 下运行。 (注意,您可以使用 yourcommand |& mail ...csh 中通过管道传输这两个描述符,但只有疯子才会使用 csh 编写脚本。)

更新:

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

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

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

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

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

关于linux - 如何在后台运行命令并在完成后通过电子邮件通知我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2962392/

相关文章:

linux - 在 bash 中复制整个驱动器上的所有照片?

python - 查找不包括测试用例的 Python 代码总行数

bash - 将 bash 变量分配为两个单词字符串的列表

linux - 连接文件名时的 Unix 日期格式(Unix Shell 脚本)

shell - Emacs shell 中的响铃

xcode - 如何在 Mac 应用程序包中对 shell 脚本可执行文件进行协同设计?

linux - 将文件中的 "{"替换为 "x "

linux - 将日志条目安排到标有日期的文件中

c++ - 在 C++ 中使用 QProcess 重定向 gnome 终端的输出

bash - 如何在 UNIX 脚本中使用 awk 作为变量