mqtt.sh 1.18 KB
#!/bin/bash
export LD_LIBRARY_PATH="/opt/stack/lib/mqtt_lib"
user=mosquitto
PIDFILE=/opt/stack/mqtt/log/mqtt.pid
pid=$(ps -u $user |tail -n 1 |awk '{print $1}')
case $1 in
 start) echo -n "Starting MQTT: "
	if [ "$pid" = "PID" ]; then
	     test -f $PIDFILE && rm $PIDFILE
	     /opt/stack/mqtt/sbin/mosquitto -d -c /opt/stack/mqtt/conf/mosquitto.conf  2>/dev/null
	     if [ $? -eq 0 ]; then
	          #ps -u $user |tail -n 1 |awk '{print $1}' 1>$PIDFILE
	          echo "success"
	     else echo "failed"
		  exit 1
	     fi
	else echo "running"
	fi
	;;
  stop) echo -n "Stoping MQTT: "
	if [ "$pid" = "PID" ]; then
	     echo "inactive"
	else kill -SIGTERM $pid 2>/dev/null && rm "$PIDFILE"
	     echo "stopped"
	fi
	;;
status) 
        if [ -s $PIDFILE ]; then
             if ps -p $(cat $PIDFILE) > /dev/null; then
                 echo "mqtt is running"
                 STATUS=0
             else echo "found PID file but not running"
                 STATUS=1
             fi
        elif [ -f $PIDFILE ]; then
             echo "PID file size is zero and not running"
             STATUS=2
        else echo "mqtt is not running"
             STATUS=3
        fi

;;
    *)  echo "not im"
esac