Search code examples
linuxbashsystemdmutt

Why mutt doesn't send emails with systemd?


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


Solution

  • Now everything works as it should. To the .service file I added:

    [Service]
    Environment=PGDATA=/path/to/my/script/checkip.sh