英文:
Why mutt doesn't send emails with systemd?
问题
我写了一个Bash脚本,用于检查我的家庭网络的外部IP是否发生了变化。如果发生变化,它会通过mutt发送一封电子邮件到我的个人电子邮件地址,通知IP已更改,并打印新的IP地址。
这个程序在命令行界面上运行正常,但在使用systemd时,程序可以正常工作(检查IP并进行比较),但无法发送电子邮件。我无法找到问题的原因,因为服务状态没有返回任何错误。
我编写了这个Bash脚本程序,它被称为CHECKIP:
第11 - 15行被注释掉,因为我想确保这些进程不会引起我的问题。第16行是我的程序的简化版本。
#!/bin/bash
last_ip=$(curl -s ifconfig.me)
echo -e "Actual IP is: $last_ip\n"
while [[ true ]]; do
actual_ip=$(curl -s ifconfig.me)
if [ $actual_ip != $last_ip ]; then
last_ip=$actual_ip
echo -e "IP has changed. The new IP address is: $actual_ip\n"
#template=$(cat /home/karol/Projekty/bash-script/checkip-msg.html)
#message=$(sed "s/{{actual_ip}}/$actual_ip/g" <<< $template)
#tmp_msg="home/karol/Projekty/bash-script/tmp/email.html"
#echo $message > $tmp_msg
#mutt -s "IP chas changed" me -e "set content_type=text/html" < "$tmp_msg"
mutt -s "IP has changed" me <<< "Your new IP address is $actual_ip"
fi
sleep 10
done
checkip.service位于/etc/systemd/system
中:
[Unit]
Description=External IP checker
After=network.target
[Service]
ExecStart=/bin/bash -lc "/home/karol/Projekty/bash-script/checkip.sh"
[Install]
WantedBy=network.target
当服务启动时,运行service checkip status
会显示:
karol@karol-P5K-E /etc/systemd/system$ service checkip status
● checkip.service - External IP checker
Loaded: loaded (/etc/systemd/system/checkip.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-10 09:11:08 CEST; 11s ago
Main PID: 28982 (checkip.sh)
Tasks: 2 (limit: 4537)
Memory: 860.0K
CPU: 63ms
CGroup: /system.slice/checkip.service
├─28982 /bin/bash /home/karol/Projekty/bash-script/checkip.sh
└─29009 sleep 10
Jul 10 09:11:08 karol-P5K-E systemd[1]: Started External IP checker.
Jul 10 09:11:09 karol-P5K-E bash[28982]: Actual IP is: X.XX.XX.XXX
当IP发生变化时,运行service checkip status
会显示:
karol@karol-P5K-E service checkip status
● checkip.service - External IP checker
Loaded: loaded (/etc/systemd/system/checkip.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-10 09:11:08 CEST; 1min 31s ago
Main PID: 28982 (checkip.sh)
Tasks: 2 (limit: 4537)
Memory: 1.2M
CPU: 185ms
CGroup: /system.slice/checkip.service
├─28982 /bin/bash /home/karol/Projekty/bash-script/checkip.sh
└─29117 sleep 10
Jul 10 09:11:08 karol-P5K-E systemd[1]: Started External IP checker.
Jul 10 09:11:09 karol-P5K-E bash[28982]: Actual IP is: X.XX.XX.XXX
Jul 10 09:12:20 karol-P5K-E bash[28982]: /home/karol/Projekty/bash-script/checkip.sh: line 8: [: !=: unary operator expected
Jul 10 09:12:31 karol-P5K-E bash[28982]: IP has changed. The new IP adress is: Y.YY.YY.YYY
英文:
I wrote a bash script that checks if the external IP of my home network has changed. If so, it sends an email via mutt to my personal email address with notification that the IP has changed and prints the new IP address.
The program works fine on CLI but when using systemd program works (checks IP and compares) but it doesn't send emails. I can't find the cause of the problem because service status does not return any error.
I wrote the bash script program. It is called CHECKIP:
the lines 11 - 15 are commented out because I wanted to make sure that these processes are not causing my problem. line 16 is a simplified version of my program.
1 #!/bin/bash
2
3 last_ip=$(curl -s ifconfig.me)
4 echo -e "Actual IP is: $last_ip\n"
5
6 while [[ true ]];do
7 actual_ip=$(curl -s ifconfig.me)
8 if [ $actual_ip != $last_ip ]; then
9 last_ip=$actual_ip
10 echo -e "IP has changed. The new IP adress is: $actual_ip\n"
11 #template=$(cat /home/karol/Projekty/bash-script/checkip-msg.html)
12 #message=$(sed "s/{{actual_ip}}/$actual_ip/g" <<< $template)
13 #tmp_msg="home/karol/Projekty/bash-script/tmp/email.html"
14 #echo $message > $tmp_msg
15 #mutt -s "IP chas changed" me -e "set content_type=text/html" < "$tmp_msg"
16 mutt -s "IP has changed" me <<< "Your new IP address is $actual_ip"
17 fi
18 sleep 10
19 done
The checkip.service is located in /etc/systemd/system
:
1 [Unit]
2 Description=External IP checker
3 After=network.target
4
5 [Service]
6 ExecStart=/bin/bash -lc "/home/karol/Projekty/bash-script/checkip.sh"
7
8 [Install]
9 WantedBy=network.target
And service checkip status
when service is started shows:
karol@karol-P5K-E /etc/systemd/system$ service checkip status
● checkip.service - External IP checker
Loaded: loaded (/etc/systemd/system/checkip.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-10 09:11:08 CEST; 11s ago
Main PID: 28982 (checkip.sh)
Tasks: 2 (limit: 4537)
Memory: 860.0K
CPU: 63ms
CGroup: /system.slice/checkip.service
├─28982 /bin/bash /home/karol/Projekty/bash-script/checkip.sh
└─29009 sleep 10
lip 10 09:11:08 karol-P5K-E systemd[1]: Started External IP checker.
lip 10 09:11:09 karol-P5K-E bash[28982]: Actual IP is: X.XX.XX.XXX
service checkip status
when IP has changed shows:
karol@karol-P5K-E service checkip status
● checkip.service - External IP checker
Loaded: loaded (/etc/systemd/system/checkip.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-10 09:11:08 CEST; 1min 31s ago
Main PID: 28982 (checkip.sh)
Tasks: 2 (limit: 4537)
Memory: 1.2M
CPU: 185ms
CGroup: /system.slice/checkip.service
├─28982 /bin/bash /home/karol/Projekty/bash-script/checkip.sh
└─29117 sleep 10
lip 10 09:11:08 karol-P5K-E systemd[1]: Started External IP checker.
lip 10 09:11:09 karol-P5K-E bash[28982]: Actual IP is: X.XX.XX.XXX
lip 10 09:12:20 karol-P5K-E bash[28982]: /home/karol/Projekty/bash-script/checkip.sh: line 8: [: !=: unary operator expected
lip 10 09:12:31 karol-P5K-E bash[28982]: IP has changed. The new IP adress is: Y.YY.YY.YYY
答案1
得分: 1
现在一切都按照应该的方式运行。在.service
文件中我添加了:
[Service]
Environment=PGDATA=/path/to/my/script/checkip.sh
英文:
Now everything works as it should. To the .service
file I added:
[Service]
Environment=PGDATA=/path/to/my/script/checkip.sh
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论