#!/bin/bash ### BEGIN INIT INFO # Provides: get_archives # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start/Stop get_archives server ### END INIT INFO # chkconfig: 35 92 1 # SysV Init script # Only for documentation DO NOT USE THIS # script settings PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin config=/usr/local/etc/get_archives.conf daemon=/usr/local/bin/get_archives serviceport=$(grep -i "^serviceport" $config | cut -d '=' -f 2) function start_service() { if ! netstat -tan | grep $serviceport | grep -Eq "ESTABLISHED|LISTEN"; then $daemon $debug & sleep 1 if netstat -tan | grep $serviceport | grep -Eq "LISTEN"; then echo "$(basename $daemon) started and accepting connections on port $serviceport" retval=0 else echo "$(basename $daemon) not started. Manual check needed" retval=2 fi else echo "FATAL ERROR : Cannot use ServicePort $serviceport for main service. Port already in use" retval=1 fi } debug="" [ "$1" == "--debug" ] && debug="--debug" && shift case $1 in start) echo "Starting $(basename $daemon)" start_service ;; stop) echo "Stopping $(basename $daemon)" echo "EXIT_THIS" | nc localhost $serviceport 2> /dev/null retval=$? if [ $retval -ne 0 ]; then echo "No instance of $daemon on port $serviceport found" fi ;; status) echo "Status $(basename $daemon)" echo "STATUS" | nc localhost $serviceport 2> /dev/null retval=$? if [ $retval -ne 0 ]; then echo "No instance of $(basename $daemon) on port $serviceport found" fi ;; reload) echo "Reloading config of $(basename $daemon)" echo "RELOAD" | nc localhost $serviceport 2> /dev/null retval=$? if [ $retval -ne 0 ]; then echo "No instance of $(basename $daemon) on port $serviceport found" else echo "STATUS" | nc localhost $serviceport 2> /dev/null fi ;; restart) echo "Stopping $(basename $daemon)" echo "EXIT_THIS" | nc localhost $serviceport 2> /dev/null retval=$? if [ $retval -ne 0 ]; then echo "No instance of $(basename $daemon) on port $serviceport found" retval=0 fi if [ $retval -eq 0 ]; then sleep 1 echo "Starting $(basename $daemon)" start_service retval=$? fi ;; *) echo "Usage : $(basename $0) {start|stop|status|restart|reload}" retval=1 ;; esac exit $retval