You are not logged in.

#1 2016-10-06 07:49:09

kevdog
Member
Registered: 2013-01-26
Posts: 102

Trying to run a service at shutdown prior to network being taken down

Hey I was wondering if I could get a heads up -- I basically trying to email myself prior to the machine or network is taken down.  Taken down is usually via a shutdown -h now statement.

I attempt first to do this with a Systemd script -- Here are my files

This is located at /etc/systemd/system/mail-at-shutdown.service

[Unit]
Description=/etc/rc.local.shutdown Compatibility
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/etc/rc.local.shutdown
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

The script /etc/rc.local.shutdown is the following:

#!/bin/bash

/bin/systemctl restart postfix.service

LAN_IP=`hostname --all-ip-addresses | awk '{print $1}'`
WAN_IP=`wget http://ipinfo.io/ip -qO -`
SERVER_NAME=`hostname -f`

echo > /root/email.txt
date >> /root/email.txt
echo >> /root/email.txt
echo "${SERVER_NAME} @ LAN IP =  ${LAN_IP} - WAN IP =  ${WAN_IP} Shutting Down" >> /root/email.txt

mail -s "${SERVER_NAME} @ ${LAN_IP} Shutting Down" -r system@xxx.com xxxxx@gmail.com < /root/email.txt
mail -r system@xxxx.com xxxxxxxxxx@vtext.com < /root/email.txt

##Debug section
cp /root/email.txt /home/kevdog/email.txt
chmod 644 /home/kevdog/email.txt
##

rm -rf /root/email.txt

sleep 30

/bin/systemctl restart postfix.service

The systemd unit file won't load on boot.   Nothing shows up with journalctl -b -u mail-at-shutdown  and here is the following:

$ sudo systemctl status mail-at-shutdown
● mail-at-shutdown.service - /etc/rc.local.shutdown Compatibility
   Loaded: loaded (/etc/systemd/system/mail-at-shutdown.service; enabled;
   Active: inactive (dead)

If I reload the service manually with the following, here is what I get:

$ sudo systemctl restart mail-at-shutdown.service
$ sudo systemctl status mail-at-shutdown
● mail-at-shutdown.service - /etc/rc.local.shutdown Compatibility
   Loaded: loaded (/etc/systemd/system/mail-at-shutdown.service; enabled;
   Active: active (exited) since Thu 2016-10-06 02:36:29 CDT; 3s ago
  Process: 2093 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 2093 (code=exited, status=0/SUCCESS)

Oct 06 02:36:29 server systemd[1]: Starting /etc/rc.local.shutdown
Oct 06 02:36:29 server systemd[1]: Started /etc/rc.local.shutdown 

If restart the service manually, the script will work creating the file locally, however no mail will be sent because during the shutdown process, the network is obviously taken down - prior to the mail  being sent via postfix.  I've tested this script by itself and everything will work as expected however I can't coordinate everything so it functions at shutdown or restart prior to the network being taken down.  In fact I don't even know if network.target is appropriate.  Possibly someone could help me with how to do everything.

I've tried putting the script in at /etc/NetworkManager/dispatcher.d/pre_down.d, however this didn't seem to affect the behavior at all.  There are bug reports that state within Networkmanager the pre-down stuff might not work anyway. https://bugzilla.gnome.org/show_bug.cgi?id=701242#c10

Offline

#2 2016-10-07 10:12:14

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: Trying to run a service at shutdown prior to network being taken down

kevdog wrote:

The systemd unit file won't load on boot.   Nothing shows up with journalctl -b -u mail-at-shutdown  and here is the following:

$ sudo systemctl status mail-at-shutdown
● mail-at-shutdown.service - /etc/rc.local.shutdown Compatibility
   Loaded: loaded (/etc/systemd/system/mail-at-shutdown.service; enabled;
   Active: inactive (dead)

This is the first problem: the service is not started at all.
You could try with:

[Unit]
Description=/etc/rc.local.shutdown Compatibility
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/etc/rc.local.shutdown
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Another problem in my opinion is to use 'systemctl restart postfix.service' in the script, at a time when the system is shutting down.

Offline

#3 2016-10-07 20:59:40

kevdog
Member
Registered: 2013-01-26
Posts: 102

Re: Trying to run a service at shutdown prior to network being taken down

Here is what I ended up using -- since I changed everything -- meaning I get a notification via email (via Postfix) and SMS when the system goes up and when it goes down.

Here is the following service unit file:

[Unit]
Description=System Email Notification at Startup and Shutdown
After=network.target postfix.service

[Service]
User=root
WorkingDirectory=/root
ExecStart=/usr/local/bin/SystemEmail.sh start
ExecStop=/usr/local/bin/SystemEmail.sh stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

I like your Wants and After statement and I'll try to make the change

Just a few words of warning to those that may find this information useful
1) The script this file calls /usr/local/bin/SystemEmail.sh runs as root
2) As found on #systemd irc on freeenode, the system unit file by default runs the file with the working path set to /. 
3) Because of the working directory being considered /, postfix may not function if you've enabled the Maildir/ option within the /etc/postfix/mainf.cf file

....
home_mailbox = Maildir/
...

4)You need to modify the WorkingDirectory to a directory which root can write to that has a subdirectory named Maildir
5)If you are using an ecryptfs for /home directory, you MUST SPECIFY the WorkingDirectory be outside the encryptfs path, because in this example the directory structure will be unavailable because it will still be encrypted on boot and after logoff.  In this example I chose /root as the working directory, however this could be any directory outside the encrypted home directory paths

For completeness this is my email script.  Please modify accordingly

#!/bin/bash

LAN_IP=`hostname --all-ip-addresses | awk '{print $1}'`
WAN_IP=`wget http://ipinfo.io/ip -qO -`
SERVER_NAME=`hostname -f`
STARTUP_SUBJECT="[${SERVER_NAME}] - System Startup: "`date`
STARTUP_MESSAGE="$SERVER_NAME @ LAN IP = ${LAN_IP} - WAN IP - ${WAN_IP} Started Successfully: "`date`
SHUTDOWN_SUBJECT="[${SERVER_NAME}] - System Shutdown: "`date`
SHUTDOWN_MESSAGE="$SERVER_NAME @ LAN IP = ${LAN_IP} - WAN IP - ${WAN_IP} Shutting Down: "`date`
EMAIL_ADDRESS="xxxxxx@gmail.com"
SENDER_ADDRESS="xxxxx@gmail.com"
SMS_NUMBER="xxxxxx@vtext.com"  #For a list of email to sms gateways please see http://www.digitaltrends.com/mobile/how-to-send-e-mail-to-sms-text/
RETVAL=0

stop()
{
   echo  $"Sending Shutdown Email "
   echo "${SHUTDOWN_MESSAGE}" |  mail -Ssendwait -Sfrom="${SENDER_ADDRESS}" -s "${SHUTDOWN_SUBJECT}" ${EMAIL_ADDRESS}
   #sleep 10
   echo "${SHUTDOWN_MESSAGE}" | mail -Ssendwait -Sfrom="${SENDER_ADDRESS}" ${SMS_NUMBER}
   #sleep 10
   RETVAL=$?

   return ${RETVAL}
}

start()
{
   echo  $"Sending Startup Email "
   echo "${STARTUP_MESSAGE}" | mail -Ssendwait -Sfrom="${SENDER_ADDRESS}" -s "${STARTUP_SUBJECT}" ${EMAIL_ADDRESS}
   #sleep 10
   echo "${STARTUP_MESSAGE}" | mail -Ssendwait -Sfrom="${SENDER_ADDRESS}" ${SMS_NUMBER}
   #sleep 10
   RETVAL=$?

   return ${RETVAL}

}

case "$1" in
start)
   start
;;
stop)
   stop
;;
status)
   echo "Not applied to service"
;;
restart)
   stop
   start
;;
reload)
   echo "Not applied to service"
;;
probe)
;;
*)
   echo "Usage: SystemEmail{start|stop|status|reload|restart}"
   exit 1
;;

esac

exit ${RETVAL}

Thanks for any help debugging this issue.

Offline

#4 2016-10-08 14:22:28

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: Trying to run a service at shutdown prior to network being taken down

You didn't say if that solution works.

I think the line

After=network.target postfix.service

could be

Wants=postfix.service
After=postfix.service

The postfix service unit has already an 'After=network.target' in it.

But that line may not be enough to warrant the network is up when the service is started.
See http://www.freedesktop.org/wiki/Softwar … workTarget

Offline

Board footer

Powered by FluxBB