Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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)

share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

Initscript for various runlevels, not crontab

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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