LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   systemd: send a mail at boot time (https://www.linuxquestions.org/questions/linux-software-2/systemd-send-a-mail-at-boot-time-4175570872/)

gargunkle 02-03-2016 07:08 AM

systemd: send a mail at boot time
 
Hi,

I have a CentOS 7 system whose IP may change sometimes. I would like to send a mail at boot time. I am using postfix. The config I have is:

[root@system ~]# cat /etc/systemd/system/mailip.service
[Unit]
Description=mailip
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/root/mailip.sh
Type=oneshot

[Install]
WantedBy=multi-user.target graphical.target

[root@system ~]# cat /root/mailip.sh
#!/bin/bash

ip addr | mail -s "system start ip config" address@isp.com
[root@system ~]#

I had run:
systemctl daemon-reload
systemctl enable mailip

If I run /root/mailip.sh manually it works. However, if I reboot the system or run "systemctl start mailip," no mail is generated. Any ideas what I could be missing?

Many thanks.

thesnow 02-03-2016 03:37 PM

Since you don't explicitly set the path for ip or mail in mailip.sh, have you checked your PATH variable, or tried adding it in your script? Similar thing has chumped me many times with cron, e.g.:

Code:

export PATH=$PATH:/path/to/ip:/path/to/mail

twork 02-03-2016 05:20 PM

One idea: change:

Code:

ip addr | mail -s "system start ip config" address@isp.com
...to:

Code:

ip addr | mail -s "system start ip config" address@isp.com >> /var/log/mailip.log 2>&1
Then if something is going wrong with invocation, you should get a record of it in /var/log/mailip.log.

If nothing shows up there, then we know that either your script isn't being called, or the problem is happening somewhere further down the process.

notjustrelational 02-03-2016 05:32 PM

This looks right:

[root@system ~]# cat /etc/systemd/system/mailip.service
[Unit]
Description=mailip
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/root/mailip.sh
Type=oneshot

[Install]
WantedBy=multi-user.target graphical.target

[root@system ~]# cat /root/mailip.sh
#!/bin/bash

ip addr | mail -s "system start ip config" address@isp.com >> /var/log/mailip.log 2>&1
[root@system ~]#

I had run:
systemctl daemon-reload
systemctl enable mailip

gargunkle 02-04-2016 09:27 AM

Hmm, still no go.

Haven't changed /etc/systemd/system/mailip.service

[root@system ~]# cat /root/mailip.sh
#!/bin/bash

/usr/sbin/ip addr | /usr/bin/mail -s "system start ip config" address@isp.com >> /var/log/mailip.log 2>&1


Works as /root/mailip.sh still (but oddly nothing gets written to /var/log/mailip.log)

-rw-r--r-- 1 root root 0 Feb 4 10:22 /var/log/mailip.log

"systemctl start mailip" doesn't generate a mail, doesn't update the log and doesn't write to maillog.

Feb 4 10:24:07 systemname systemd: Starting mailip...
Feb 4 10:24:07 systemname systemd: Started mailip.

It's odd.

twork 02-04-2016 09:56 PM

Okay, so it looks like our init process isn't calling that script at all, but just to be sure, what if we set aside the present script and replace it with:

#!/bin/bash
echo "testing: $0" > /tmp/testing-${0}.out

Then if that tmp file doesn't show up, we know that the trouble is somewhere in systemd, and not something weird with the mail setup, or the way the mail and systemd areas interact.


All times are GMT -5. The time now is 08:11 PM.