#!/bin/sh

IS_ALIVE=$( netstat -tulpn | grep -w '127.0.0.1:5020' )

if [ "$IS_ALIVE" == "" ]; then
   CHECK_USER=$( cat /etc/passwd | cut -d ':' -f1 | grep -w 'onappsshws' )
   if [ ! -z "$CHECK_USER" ]; then
      IS_PIDS=$( ps aux -u sonappsshws | grep -w '[/]usr/local/onapp/hosters/sshwebsocket/bin/sshwebsocket --run' | awk '{print $2}' )
      if [ ! -z "$IS_PIDS" ]; then
         while read -r PID; do
            if [ $PID -eq $PID 2> /dev/null ]; then
               kill -9 $PID
            fi
         done <<< "$IS_PIDS"
      fi
      exec su onappsshws -s /bin/sh -c "nohup /usr/local/onapp/hosters/sshwebsocket/bin/sshwebsocket --run > /dev/null 2>&1 &"
   fi
fi
