4

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.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
linuxnewbee
  • 998
  • 2
  • 10
  • 23

3 Answers3

14

Place your shell script in /etc/init.d with executable permission and symlink name should start with K##. If you want to execute your script at first place immediately after shut down then name it with K00scriptname. Script started will K will be executed first based on ascending order then script with S.

ln -s /etc/init.d/script /etc/rc0.d/K00scriptname

Shutdown command will send the stop signal to script, your script (K00scriptname) should have stop function like example

stop()
{
  echo "executing scriptname"
  "Your script logic"
}
case "$1" in
  stop)
    stop
    ;;
esac

Most important, K00scriptname will execute only if there would be lock file present in /var/lock/subsys folder, so do "touch /var/lock/subsys/scriptname" then check by doing shutdown.

Alexej Magura
  • 4,833
  • 3
  • 26
  • 40
Mahattam
  • 5,405
  • 4
  • 23
  • 33
1

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
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • 1
    As I told you before the script has proper permission. However I have double checked it by adding the permission again. Still no luck :( – linuxnewbee Sep 19 '13 at 09:54
  • @tapasmishra Please see update. And you can also debug your script by adding `set -x` at the beginning of it. You should at least see the lines executed in the script. – konsolebox Sep 19 '13 at 09:56
  • @tapasmishra With set -x at the beginning of the script (after the #! header) you should have seen the lines of the script being shown before they are executed. If not, your script is probably not running at all (unless stderr is suppressed by the rc system which is unlikely). If it does run but still the mailing doesn't work, please update the thread with those debug messages you see so people might give ideas about it. – konsolebox Sep 19 '13 at 10:47
  • can you define the PATH at the start of the script, rather than use absolute paths for commands? @konsolebox – 170730350 Nov 15 '18 at 20:24
  • @Saichovsky If you're referring to OP's example as an example, yes. But I don't see how that would make a difference. – konsolebox Dec 03 '18 at 20:47
  • @konsolebox no difference, save for the possibly painstaking task of confirming the absolute paths of each single command in your script – 170730350 Dec 07 '18 at 12:35
  • @Saichovsky I agree, unless you want to replace the initialization of PATH and have it hardcoded in your script. I find that a bad practice unless 1) you intend to keep the PATH static for the script, 2) you're actually making an init script, or 3) you plan to append another path. Also take note that the use of absolute paths was not a mandatory suggestion from me, but just a way to avoid + discover an issue that may be related to PATH. Use of absolute path is required if you have to be explicit at targetting a specific binary, or if you want to make sure it runs even if PATH is uninitialized. – konsolebox Dec 13 '18 at 08:59
  • @Saichovsky I didn't notice this script was indeed about an init script (been 5 years), so my suggestion to use explicit (absolute) path was correct for that's what should always be done. Specifying PATH in an init script - LIKE THAT - is bad practice. After all you're NOT initializing PATH for the system. It can also alter PATH globally by mistake. – konsolebox Dec 13 '18 at 09:10
  • @konsolebox I hear you on the bad practice bit. But as long as you do not export the variable, I think it is unlikely to affect the system PATH. Bad practice still it is :-) – 170730350 Dec 13 '18 at 09:16
0

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

The symlink name should begin with a S - for Start (K for Kill)

The two-digit specifies the order of execution for your script, the lowest numbered being execute first.

suhas
  • 733
  • 5
  • 13