ctlscript.sh 3.96 KB
#!/bin/bash
# Allow only root execution
if [ `id|sed -e s/uid=//g -e s/\(.*//g` -ne 0 ]; then
    echo "This script requires root privileges"
    exit 1
fi
#set -x
export LD_LIBRARY_PATH="/opt/stack/lib/mqtt_lib/:/opt/stack/lib/"

#PID
MQTTPID=/opt/stack/mqtt/log/mqtt.pid
POSTGRESPID=/opt/stack/postgres/postgres/mongodb.pid
MONGOPID=/opt/stack/mongodb/mongodb/tmp/mongodb.pid
JBOSSPID=/opt/stack/wildfly/wildfly/standalone/tmp/jboss.pid
NGINX_WILDFLY_PID=/opt/stack/nginx/logs/nginx.wildfly.pid
NGINX_CHAT_PID=/opt/stack/nginx/logs/nginx.chat.pid

#CMD
# TEST 
CMD_TEST_WILDFLY="/opt/stack/script/8080status.sh"
CMD_TEST_NGINX_WILDFLY="/opt/stack/nginx/sbin/nginx -q -t -c /opt/stack/nginx/conf/nginx.wildfly.conf"
CMD_TEST_NGINX_CHAT="/opt/stack/nginx/sbin/nginx -q -t -c /opt/stack/nginx/conf/nginx.chat.conf"

# START
CMD_START_MONGODB="/opt/stack/mongodb/ctlscript.sh start mongodb"
CMD_START_POSTGRES="/opt/stack/postgres/ctlscript.sh start postgresql"
CMD_START_MQTT="/opt/stack/script/mqtt.sh start"
CMD_START_ADMIN="/opt/stack/admin/ctlscript.sh start wildfly"
CMD_START_RUNTIME="/opt/stack/runtime/ctlscript.sh start wildfly"
CMD_START_NGINX_WILDFLY="/opt/stack/nginx/sbin/nginx  -c /opt/stack/nginx/conf/nginx.wildfly.conf"
CMD_START_NGINX_CHAT="/opt/stack/nginx/sbin/nginx  -c /opt/stack/nginx/conf/nginx.chat.conf"

# STOP
CMD_STOP_MONGODB="/opt/stack/mongodb/ctlscript.sh stop mongodb"
CMD_STOP_POSTGRES="/opt/stack/postgres/ctlscript.sh stop postgresql"
CMD_STOP_ADMIN="/opt/stack/admin/ctlscript.sh stop wildfly"
CMD_STOP_RUNTIME="/opt/stack/runtime/ctlscript.sh stop wildfly"
CMD_STOP_NGINX_WILDFLY="/opt/stack/nginx/sbin/nginx -s stop  -c /opt/stack/nginx/conf/nginx.wildfly.conf"
CMD_STOP_NGINX_CHAT="/opt/stack/nginx/sbin/nginx -s stop  -c /opt/stack/nginx/conf/nginx.chat.conf"
CMD_STOP_MQTT="/opt/stack/script/mqtt.sh stop"

help()
{
        cat <<EOF

help       - this screen
start      - start the service(s)
EOF
}

do_start()
{
#start mongodb
#set -x 
  $CMD_START_MONGODB

#start
  $CMD_START_POSTGRES

#start mqtt
  $CMD_START_MQTT 
  
#start runtime
  $CMD_START_RUNTIME

#start admin
  $CMD_START_ADMIN

#start nginx for wildfly
  if [ -s $NGINX_WILDFLY_PID ]; then
       if ps -p $(cat $NGINX_WILDFLY_PID) > /dev/null; then
          echo "nginx for wildfly: is running"
	  STATUS=0
       else echo "nginx for wildfly: found PID file but not run"
	    echo "rm $NGINX_WILDFLY_PID" && rm $NGINX_WILDFLY_PID
	  STATUS=1
       fi
  elif [ -f $NGINX_WILDFLY_PID ]; then
       echo "nginx for wildfly: PID file size is zero"
       echo "rm $NGINX_WILDFLY_PID" && rm $NGINX_WILDFLY_PID
	  STATUS=1        
  else STATUS=1
  fi
  if [ $STATUS -eq 1 ]; then
       echo "nginx for wildfly: starting" && $CMD_TEST_NGINX_WILDFLY
       if [ $? -eq 0 ]; then
 	    $CMD_START_NGINX_WILDFLY
	    if [ $? -ne 0 ]; then
		 return $?
	    fi
       fi
  fi

#start nginx for chat
  if [ -s $NGINX_CHAT_PID ]; then
       if ps -p $(cat $NGINX_CHAT_PID) > /dev/null; then
          echo "nginx for chat: is running"
          STATUS=0
       else echo "nginx for chat: found PID file but not run"
            echo "rm $NGINX_CHAT_PID" && rm $NGINX_CHAT_PID
          STATUS=1
       fi
  elif [ -f $NGINX_CHAT_PID ]; then
       echo "nginx for chat: PID file size is zero"
       echo "rm $NGINX_CHAT_PID" && rm $NGINX_CHAT_PID
          STATUS=1
  else STATUS=1
  fi
  if [ $STATUS -eq 1 ]; then
       echo "nginx for chat: starting" && $CMD_TEST_NGINX_CHAT
       if [ $? -eq 0 ]; then
            $CMD_START_NGINX_CHAT
            if [ $? -ne 0 ]; then
                 return $?
            fi
       fi
  fi

#check wildfly start completely
  for i in $(seq 1 10)
  do $CMD_TEST_WILDFLY
	if [ $? -ne 0 ]; then
	  STATUS=1
	  sleep 5
	else 
	  STATUS=0
	  break
	fi
  done
  return $STATUS
}

do_stop()
{
  $CMD_STOP_NGINX_WILDFLY
  $CMD_STOP_NGINX_CHAT
  $CMD_STOP_ADMIN
  $CMD_STOP_RUNTIME
  $CMD_STOP_POSTGRES
  $CMD_STOP_MQTT
  $CMD_STOP_MONGODB

}

#main
case $1 in
start)	do_start
	exit $?;;
stop)	do_stop;;
   *)	help
esac