0

Which is the best way to run a script upon server restart and when server is up again?

e.g. When services will be unavailable:

irc_notify "Server is going down"

when all services are available:

irc_notify "Server is up again"

Server shutdown may be triggered by various scripts, so I don't want to use shutdown command with parameters.

Geraint Jones
  • 2,503
  • 16
  • 19
takeshin
  • 1,471
  • 3
  • 21
  • 28

3 Answers3

4

Create the following script /etc/init.d/irc_notify

### BEGIN INIT INFO
# Provides:          irc_notifications
# Required-Start:    $network
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: IRC Notifications
# Description:       Simple script to send notifications to IRC
### END INIT INFO
#!/bin/bash
case "$1" in
  start)
       irc_notify "Server is up again"
  stop)
       irc_notify "Server is going down"
esac

Once that is done run (for CentOS/RHEL) chkconfig add irc_notify && chkconfig irc_notify on or (for debian/ubuntu) update-rc.d irc_notify start 3 5 stop 0 1 2 6

Geraint Jones
  • 2,503
  • 16
  • 19
  • looks like the right command for Ubuntu needs dots: `update-rc.d irc_notify start 3 5 . stop 0 1 2 6 .`, but I still get the warnings and the messages do not show. – takeshin Sep 29 '10 at 12:39
1

Create a script in /etc/init.d, make it executable, then check how to execute it on the different runlevels

update-rc.d

You probably are interested in runlevel 0 (shutdown) and 3 or 5 (normal runlevel after the startup)

marcoc
  • 748
  • 4
  • 10
0

Initscript for various runlevels, not crontab

ghloogh
  • 1,049
  • 5
  • 9