linuxbashshellcentosrunlevel

How to run a Shell Script before shutdown on CentOS


I want to send an email when the system is going to shutdown to an email ID. I have CentOS 6.4. Below is my Script.

cat /ect/init.d/sendshtmail

#!/bin/bash

EMAIL="example@example.com"
SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown"


SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down.

LOCKFILE=/var/lock/subsys/SystemEmail
echo "${SHUTDOWNBODY}" | mutt -s "${SHUTDOWNSUBJECT}" ${EMAIL}

It has the appropriate permission. While running it manually it's working perfectly. I have just symlinked it to /etc/rc0.d/ folder. By issuing below command.

 ln -s /etc/init.d/sendshtmail /etc/rc0.d/K00sendshtmail

But the script is not sending any email during shutdown. Thanks in Advance.


Solution

  • Try to set executable permissions for your script. Sometimes you need to do that to activate it.

    chmod 755 /etc/init.d/sendshtmail
    

    Also try to use absolute paths for your command, while quoting the other variable as well.

    echo "${SHUTDOWNBODY}" | /usr/bin/mutt -s "${SHUTDOWNSUBJECT}" "${EMAIL}"
    

    Another attempt is to switch your user to your current user e.g.

    echo "${SHUTDOWNBODY}" | su -l -c "/usr/bin/mutt -s \"${SHUTDOWNSUBJECT}\" \"${EMAIL}\"" yourusername