我有以下命令需要很长时间才能运行(几个小时)。我想让它成为一个后台进程,并让它在完成后给我发一封电子邮件。
对于顶上的樱桃,它遇到的任何错误都应该在发生错误时写入文本文件吗?
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(sh
、bash
、ksh
...),据我所知。因此,在 #!/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/