Script: Email notifications when machine is free
outline¶
login to free SMTP server(qq, google) to send email to others.
send email using command line¶
- check
ping mail.ustc.edu.cn
- MUST : set gmail open to support SMTP and apppassword
- or config qq email
IMAP, POP3 or SMTP¶
mailutils¶
According to ref1.
sudo apt-get install mailutils
$ echo "Hello world" | mail -s "Test" [email protected]
send but the same, more crazy thing is the -v
and -d
flag is not supported. and --debug-level=trace0
isn't recognized. many same question in StackOverflow
sudo tail -n 30 /var/log/mail.log
or mail.error
show more info.
ssmtp¶
We try ref2 ssmtp
, sudo vim /etc/ssmtp/ssmtp.conf
TLS_CA_FILE=/etc/pki/tls/certs/ca-bundle.crt
root=[email protected]
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
AuthUser=shaojieemail
AuthPass={apppassword}
FromLineOverride=YES
UseSTARTTLS=Yes
UseTLS=YES
hostname=snode6
The config get work but not well configed, e.g., TLS_CA_FILE
sending a email using gmail took about 13 mins.
$ ssmtp 943648187@qq.com < mail.txt
......
[->] Received: by snode6 (sSMTP sendmail emulation); Wed, 06 Sep 2023 15:42:05 +0800
[->] From: "Shaojie Tan" <[email protected]>
[->] Date: Wed, 06 Sep 2023 15:42:05 +0800
[->] test server email sending
[->]
[->] .
[<-] 250 2.0.0 OK 1693986323 5-20020a17090a1a4500b0026b4ca7f62csm11149314pjl.39 - gsmtp
[->] QUIT
[<-] 221 2.0.0 closing connection 5-20020a17090a1a4500b0026b4ca7f62csm11149314pjl.39 - gsmtp
sendmail¶
$ sendmail [email protected] < mail.txt
sendmail: Authorization failed (535 5.7.8 https://support.google.com/mail/?p=BadCredentials e7-20020a170902b78700b001c0c79b386esm8725297pls.95 - gsmtp)
get to work after well config gmail setting.
Speed Compare¶
command | snode6 time(mins) | icarus1 |
---|---|---|
4 | 1s | |
ssmtp | 13 | |
sendmail | 6 |
send email by python¶
ref using QQ apppassword and python.
email notifications¶
- Create a Bash Script: Create a Bash script that checks the CPU usage and sends an email if it's below 30%. For example, create a file named
cpu_check.sh
:
#!/bin/bash
# Get CPU usage percentage
cpu_usage=$(top -b -n 1 | grep '%Cpu(s):' | awk '{print $2}' | cut -d'.' -f1)
echo "cpu_usage : ${cpu_usage} on $(hostname)"
# Check if CPU usage is below 30%
if [ "$cpu_usage" -lt 30 ]; then
echo "beyond threshold : ${cpu_usage} on "
# Send an email
echo "CPU usage is ${cpu_usage} below 30% on $(hostname)" | mail -s "Low CPU Usage Alert on $(hostname)" [email protected]
fi
Make the script executable:
Modify [email protected]
with your actual email address.
- Schedule the Script: Use the
cron
scheduler to run the script at regular intervals. Edit your crontab by running:
Add an entry to run the script, for example, every 5 minutes:
*/5 * * * * /staff/shaojiemike/test/cpu_check.sh >> /staff/shaojiemike/test/cpu_check.log
# Run every 15 minutes during working hours (9 am to 7 pm)
*/15 9-19 * * * /path/to/your/script.sh
Replace /path/to/cpu_check.sh
with the actual path to your Bash script.
- Save and Exit: Save the crontab file and exit the text editor.
Now, the script will run every 5 minutes (adjust the cron schedule as needed) and send an email notification if the CPU usage is below 50%. You should receive an email when the condition is met.
Please note that this is a basic example, and you can modify the script to include more details or customize the notification further as needed. Additionally, ensure that your server is configured to send emails; you may need to configure SMTP settings for the mail
or sendmail
command to work correctly.
需要进一步的研究学习¶
暂无
遇到的问题¶
暂无
开题缘由、总结、反思、吐槽~~¶
参考文献¶
上面回答部分来自ChatGPT-3.5,没有进行正确性的交叉校验。
无