0

I have SLES 12 SP1 Servers in my company. I have done 2 seperate configurations to alert me whenever the server undergoes a restart, to be more explicit, an email at the time of server shuts down and another one when it boots up. Alert configured to send email during startup is working fine.

But Unfortunately I am getting the shutdown alert not during the shutdown process but after the server Starts Up. Some times it will not even get generated.

Where should I make the changes to send email immediately during reboot or shutdown.

Scripts.

/etc/systemd/system/ShutDownMail.service

Description=Sending Alert Emails on System shutdown.
Requires=network.target
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/Scripts/BootMail.sh shutdown

[Install]
WantedBy=multi-user.target

/Scripts/BootMail.sh

#! /bin/sh
EMAIL="Raneesh@mycomp.com"
SUBJECT="[$HOSTNAME] - System $1"

if [ "$1" = startup ]
then
    ACTION="started successfully at"
else
    ACTION="is shutting down. The Shutdown process started at"
fi

BODY="This is an automated message to notify you that %s %s.\nDate and Time: %s\n"

printf "$BODY" "$HOSTNAME" "$ACTION" "$(date)" | mail -s "${SUBJECT}" "${EMAIL}"

Alert Emails I received at 10:59 AM. Please note occasionally the Shutdown Alert Email will not be generated. The email process might be killed before sending the email

Shutdown ALert Email

Startup alert Email


These are the emails on executing init 6.

  • 1
    Shooting in the dark: how about moving that script from ExecStop to ExecStart? – RudiC Oct 24 '18 at 9:05
  • ExecStop is used when you want something executed when the service is stopped. I guess @RudiC suggestion makes sense. – Pierre-Alain TORET Oct 24 '18 at 9:28
  • @RudiC.and and Pierre-Alain TORET. Thanks for your comments. But, it will defeat the purpose. Instead of sending the email during the shutdown sequence, it will send an email during the system booting with booting timestamp. – Raneesh Oct 24 '18 at 10:02
  • @Pierre-Alain TORET. – Raneesh Oct 24 '18 at 10:23
  • Isn't is possible that the process responsible of sending emails would be already stopped ? – Pierre-Alain TORET Oct 24 '18 at 12:24
0

Most mail programs will simply put the message in a queue, and a separate process will check the queue and send the mail. You need to therefore also trigger that process. For example, if using sendmail, add to your script a sudo sendmail -qf to try to flush the queue, or as you say in the comments a postfix flush. Perhaps an added After=sendmail.service or the postfix equivalent will also ensure the queue handling daemon is still running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.