init script installation added and version updated
[brisk.git] / bin / brisk-init.sh
diff --git a/bin/brisk-init.sh b/bin/brisk-init.sh
new file mode 100755 (executable)
index 0000000..a6aee76
--- /dev/null
@@ -0,0 +1,61 @@
+#!/bin/sh -e
+### BEGIN INIT INFO
+# Provides:          brisk
+# Required-Start:       $local_fs $remote_fs $network $time
+# Required-Stop:        $local_fs $remote_fs $network $time
+# Default-Start:        2 3 4 5
+# Default-Stop:         0 1 6
+# Short-Description: manage brisk daemon
+### END INIT INFO
+
+BPATH="xx/home/nastasi/web/brisk"
+PPATH="xx/home/nastasi/brisk-priv"
+# screen suffix
+SSUFF="xxbrisk"
+BUSER="xxwww-data"
+# seconds to wait exit of the process
+WAITLOOP_MAX=5
+
+#
+#  MAIN
+#
+NL="
+"
+TB="   "
+# scr_old="$(screen -ls | sed "s/^[ ${TB}]*//g;s/[ ${TB}]\+/ /g" | cut -d ' ' -f 1 | grep "\.${SSUFF}$")"
+# echo "[$scr_old]"
+
+case "$1" in
+    stop)
+        #
+        #  if .pid file exists try to shutdown the process
+        if [ -f "${PPATH}/brisk.pid" ]; then
+            pid_old="$(cat "${PPATH}/brisk.pid")"
+            kill -TERM $pid_old
+            killed=0
+            for i in $(seq 1 $WAITLOOP_MAX); do
+                sleep 1
+                if ! kill -0 $pid_old 2>/dev/null ; then
+                    killed=1
+                    break
+                fi
+            done
+            if [ $killed -eq 0 ]; then
+                kill -KILL $pid_old 2>/dev/null
+            fi
+        fi
+        ;;
+
+    start)
+        su - ${BUSER} -c 'cd '"$BPATH"'/spush ; screen -d -m -S '"${SSUFF}"' bash -c '"'"'while [ 1 ]; do ./brisk-spush.php \| grep "IN LOOP" ; if [ $? -eq 0 ]; then break ; fi ; done'"'"
+        ;;
+    restart)
+        $0 stop
+        sleep 3
+        $0 start
+        ;;
+    *)
+        echo "Usage: $0 {start|stop|restart}" >&2
+        exit 1
+        ;;
+esac