![从 /etc/profile 文件发送邮件](https://linux22.com/image/1059966/%E4%BB%8E%20%2Fetc%2Fprofile%20%E6%96%87%E4%BB%B6%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6.png)
我在文件中添加了一个电子邮件命令,/etc/profile
如下所示:
echo test | mail -s "test mail" [email protected] &
该行末尾的&
用于在后台运行邮件命令以免增加登录提示的延迟。
一切运行良好,但如果我按下,Enter我可以在提示符下看到邮件命令的结果:
[1]+ Done echo test | mail -s "test mail" [email protected]
如果我删除&
行尾的,问题就不会出现,但我在后台没有该命令。
答案1
尝试将其放入子 shell 中:
`echo test | mail -s "test mail" [email protected] &`
它应该可以完成这项工作。
同时重定向两个对我来说也有效stdout
:stderr
echo test | mail -s "test mail" [email protected] &> /dev/null &
答案2
尝试添加一个,!
除此之外,这样&
你的命令不仅可以在后台执行,而且还可以被你当前的shell所拒绝:
echo test | mail -s "test mail" [email protected] &!
这对我使用 zsh 的方法有效。