![](/img/trans.png)
[英]Shell script to run a command and send the commands output as a report in email
[英]Email the output of a shell script
我有一個 shell 腳本 (script.sh),我在其中運行了一個簡單的 python 網絡爬蟲
python3 script.py
當我在 cron 作業中運行這個 shell 腳本時,如果出現問題,我希望通過電子郵件收到通知。 我在 stackoverflow 上找到了這段代碼,我把它放在了我的 .sh 腳本的底部:
./script.sh 2>&1 | tee output.txt | mail -s "Script log" email@address.com
問題是腳本現在似乎在循環; 它應該只需要幾秒鍾,現在運行需要幾分鍾,我的郵箱中收到 10-20 封電子郵件。 這些電子郵件的內容各不相同,大多數情況下電子郵件是空的,但有時它們包含以下消息:
./script.sh: 4: ./script.sh: Cannot fork
或者:
mail: Null message body; hope that's ok
我不確定這里出了什么問題。 我能做些什么來解決這個問題?
腳本運行,運行python,然后再次調用自己,直到它耗盡資源並且無法fork。
.sh 應包含:
python3 script.py 2>&1 | tee output.txt | mail -s "Script log" email@address.com
假設您實際上想在 pwd 所在的任何文件夾中創建 output.txt ……否則您可以完全忽略它。
或者,您可以在 crontab 中配置您的 MAILTO(請參閱https://www.cyberciti.biz/faq/linux-unix-crontab-change-mailto-settings/ )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.