forever init script
forever init script

--- /dev/null
+++ b/iotcc-server-monitoring.sh
@@ -1,1 +1,153 @@
+#!/bin/sh
 
+FILE=`basename "$0"`
+APP_NAME="IoTCC server monitoring"
+APP_DIR="/opt/iotcc-server-monitoring"
+NODE_APP="index.js"
+CONFIG_DIR="$APP_DIR"
+PID_DIR="$APP_DIR/pid"
+PID_FILE="$PID_DIR/app.pid"
+LOG_DIR="$APP_DIR/log"
+LOG_FILE="$LOG_DIR/forever.log"
+APP_LOG_FILE="$LOG_DIR/app.log"
+APP_ERR_FILE="$LOG_DIR/err.log"
+NODE_EXEC="/usr/bin/forever"
+
+USAGE="Usage: $0 {start|stop|restart|status} [--force]"
+FORCE_OP=false
+
+pid_file_exists() {
+    [ -f "$PID_FILE" ]
+}
+
+get_pid() {
+    echo "$(cat "$PID_FILE")"
+}
+
+is_running() {
+    PID=$(get_pid)
+    ! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]
+}
+
+start_it() {
+    mkdir -p "$PID_DIR"
+    mkdir -p "$LOG_DIR"
+
+    echo "Starting $APP_NAME"
+    FOREVER_ROOT="$APP_DIR/.forever" $NODE_EXEC -s -a -l $LOG_FILE -o $APP_LOG_FILE -e $APP_ERR_FILE --pidFile $PID_FILE start "$APP_DIR/$NODE_APP"
+    while [ ! -f $PID_FILE ]
+    do
+        sleep 1
+    done
+}
+
+stop_process() {
+    FOREVER_ROOT="$APP_DIR/.forever" $NODE_EXEC stop "$APP_DIR/$NODE_APP"
+}
+
+remove_pid_file() {
+    echo "Removing pid file"
+    rm -f "$PID_FILE"
+}
+
+start_app() {
+    if pid_file_exists
+    then
+        if is_running
+        then
+            PID=$(get_pid)
+            echo "$APP_NAME running with pid $PID"
+            exit 1
+        else
+            echo "$APP_NAME stopped, but pid file exists"
+            if [ $FORCE_OP = true ]
+            then
+                echo "Forcing start anyway"
+                remove_pid_file
+                start_it
+            fi
+        fi
+    else
+        start_it
+    fi
+}
+
+stop_app() {
+    if pid_file_exists
+    then
+        if is_running
+        then
+            echo "Stopping $APP_NAME ..."
+            stop_process
+            remove_pid_file
+            echo "$APP_NAME stopped"
+        else
+            echo "$APP_NAME already stopped, but pid file exists"
+            if [ $FORCE_OP = true ]
+            then
+                echo "Forcing stop anyway ..."
+                remove_pid_file
+                echo "$APP_NAME stopped"
+            else
+                exit 1
+            fi
+        fi
+    else
+        echo "$APP_NAME already stopped, pid file does not exist"
+        exit 1
+    fi
+}
+
+status_app() {
+    if pid_file_exists
+    then
+        if is_running
+        then
+            PID=$(get_pid)
+            echo "$APP_NAME running with pid $PID"
+        else
+            echo "$APP_NAME stopped, but pid file exists"
+        fi
+    else
+        echo "$APP_NAME stopped"
+    fi
+}
+
+case "$2" in
+    --force)
+        FORCE_OP=true
+    ;;
+
+    "")
+    ;;
+
+    *)
+        echo $USAGE
+        exit 1
+    ;;
+esac
+
+case "$1" in
+    start)
+        start_app
+    ;;
+
+    stop)
+        stop_app
+    ;;
+
+    restart)
+        stop_app
+        start_app
+    ;;
+
+    status)
+        status_app
+    ;;
+
+    *)
+        echo $USAGE
+        exit 1
+    ;;
+esac
+

--- a/package.json
+++ b/package.json
@@ -4,6 +4,7 @@
   "main": "index.js",
   "dependencies": {
     "command_runner": "^1.2.0",
+    "forever-monitor": "^1.7.1",
     "mqtt": "^2.4.0"
   },
   "devDependencies": {},

comments