Forum

Simple Start/Stop/Restart script

ajayahmed
18 August 2010, 13:23
This script can be placed in your init.d folder to start as a service. I have it in /usr/local/sbin as it allows me to execute from any folder.

Also, please remember to chmod +x h.sh after saving the file in order to make it executable.

Usage: h.sh start | stop | restart

Produces similar output to service iptables start

h.sh
#!/bin/sh

# Full path to Hiawatha executable
SERVER_BIN=/usr/local/sbin/hiawatha

# Location of Hiawatha PID:
SERVER_PIDFILE=/var/run/hiawatha.pid

# Location of this scripts PID - you can leave this as default:
SCRIPT_PIDFILE=/tmp/h_script.pid

hiawatha_start() {
echo -n "Starting Hiawatha: "
if [ -f $SERVER_PIDFILE ]; then
PID='cat $SERVER_PIDFILE'
echo -e "[`tput setaf 1`FAILED`tput setaf 7`]"
echo -e " Server already running: [" $(pidof $SERVER_BIN) "]"
exit 2;
else
echo $$ > $SCRIPT_PIDFILE;
#while [ 1 ]; do
if [ ! $(pidof $SERVER_BIN) ]; then
$SERVER_BIN
sleep 1
if [ ! $(pidof $SERVER_BIN) ]; then
echo -e "[`tput setaf 3` WARN `tput setaf 7`]"
echo "Could not locate PID for ${SERVER_BIN}. If the server is running, please update $0 with the current PID file"
rm $SCRIPT_PIDFILE
exit;
fi
echo `pidof $SERVER_BIN` > $SERVER_PIDFILE
fi
#sleep 1;
#done;
fi
echo -e "[`tput setaf 2` OK `tput setaf 7`]"
}

hiawatha_stop() {
echo -n "Stopping Hiawatha: "
if [ -r $SERVER_PIDFILE ]; then
kill -15 `cat $SERVER_PIDFILE`
sleep 1
rm $SERVER_PIDFILE
echo -e "[`tput setaf 2` OK `tput setaf 7`]"
else
killall $SERVER_BIN &> /dev/null
echo -e "[`tput setaf 3` WARN `tput setaf 7`]"
echo "Hard kill - Server PID not found"
fi
}

hiawatha_restart() {
echo "Restarting Server..."
hiawatha_stop
sleep 1
hiawatha_start
}

case "$1" in
'start')
hiawatha_start
;;
'stop')
hiawatha_stop
;;
'restart')
hiawatha_restart
;;
*)
echo "Usage: $0 start|stop|restart"
esac


Disclaimer: Use at own risk.
Hugo Leisink
18 August 2010, 21:20
Thanks, but the Hiawatha package already contains a start and stop script. It's located in the extra directory inside the source package.
This topic has been closed.