diff --git a/ssh-tunnel-manager.sysv-init.sh b/ssh-tunnel-manager.sysv-init.sh index 4aa714812f216311ab2f9531f31c5220404ff48f..75c4de55a4e15f1c5a0d0bda37f1d039a28dd967 100644 --- a/ssh-tunnel-manager.sysv-init.sh +++ b/ssh-tunnel-manager.sysv-init.sh @@ -19,48 +19,69 @@ # Description: SSH-Tunnel-Manager to manager multiple SSH connections to different servers. ### END INIT INFO -# Source function library. -#. /etc/rc.d/init.d/functions +. /lib/lsb/init-functions -exec="/usr/sbin/ssh-tunnel-manager" -prog="ssh-tunnel-manager" -config="/etc/ssh-tunnel-manager.conf" +NAME=ssh-tunnel-manager +USER=root +DAEMON="/usr/local/bin/${NAME}" +CONFIG="/etc/ssh-tunnel-manager.conf" +PIDFILE="/var/run/${NAME}.pid" +# Overrides +[ -f "/etc/default/$NAME" ] && . /etc/default/$NAME -start() { - [ -x $exec ] || exit 5 - [ -f $config ] || exit 6 - $exec start -} +# If the daemon is not there, then exit. +test -x $DAEMON || exit 5 -stop() { - $exec stop -} - -restart() { - $exec restart -} - -rh_status() { - $exec status -} - - -case "$1" in - start) - $1 - ;; - stop) - $1 - ;; - restart) - $1 - ;; - status) - rh_status - ;; - *) - echo $"Usage: $0 {start|stop|status|restart}" - exit 2 -esac -exit $? \ No newline at end of file +case $1 in + start) + # Checked the PID file exists and check the actual status of process + if [ -e $PIDFILE ]; then + status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?" + # If the status is SUCCESS then don't need to start again. + if [ $status = "0" ]; then + exit # Exit + fi + fi + # Start the daemon. + log_daemon_msg "Starting the process" "$NAME" + # Start the daemon with the help of start-stop-daemon + # Log the message appropriately + if start-stop-daemon --start --chuid $USER --background --oknodo --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $CONFIG; then + log_end_msg 0 + else + log_end_msg 1 + fi + ;; + stop) + # Stop the daemon. + if [ -e $PIDFILE ]; then + status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?" + if [ "$status" = 0 ]; then + start-stop-daemon --stop --retry=TERM/60/KILL/5 --quiet --oknodo --pidfile $PIDFILE + /bin/rm -rf $PIDFILE + fi + else + log_daemon_msg "$NAME process is not running" + log_end_msg 0 + fi + ;; + restart) + # Restart the daemon. + $0 stop && sleep 2 && $0 start + ;; + status) + # Check the status of the process. + if [ -e $PIDFILE ]; then + status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $? + else + log_daemon_msg "$NAME Process is not running" + log_end_msg 0 + fi + ;; + *) + # For invalid arguments, print the usage message. + echo "Usage: $0 {start|stop|restart|status}" + exit 2 + ;; +esac \ No newline at end of file