我有以下命令,需要很长时间才能运行(几个小时)。我想将它作为后台进程,并在完成后向我发送电子邮件。
对于顶部的樱桃,遇到的任何错误都应该在发生错误时写入文本文件?
find . -type f -name "*.rm" -exec ./rm2mp3.sh \{} \; -exec rm \{} \;
我如何使用上述命令执行此操作?
答案 0 :(得分:9)
yourcommand 2>&1 | mail -s "yourcommand is done" yourname@example.com
2>&1
位表示“让我的错误像常规输出一样”,其余的则说“采取常规输出并将其邮寄给我一个不错的主题行”
请注意,它不适用于csh
系列shell,只适用于Bourne(sh
,bash
,ksh
...),AFAIK。因此,请在#!/bin/sh
或#!/bin/bash
下运行。 (N.B.您可以使用csh
在yourcommand |& mail ...
中管道两个描述符,但只有一个疯子使用csh
编写脚本。)
<强>更新强>
如何将错误写入日志文件,然后在完成时发送电子邮件给我自己?
如果您只是通过电子邮件发送已完成的事实,
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