SystemV service file for remote fanfix
authorNiki Roo <niki@nikiroo.be>
Sun, 26 Nov 2017 18:02:56 +0000 (19:02 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 26 Nov 2017 18:02:56 +0000 (19:02 +0100)
fanfix.sysv [new file with mode: 0755]

diff --git a/fanfix.sysv b/fanfix.sysv
new file mode 100755 (executable)
index 0000000..2956d89
--- /dev/null
@@ -0,0 +1,92 @@
+#!/bin/sh
+#
+# fanfix       This starts the Fanfix remote service.
+#
+# chkconfig: 2345 10 90
+# description: Starts the Fanfix remote service
+#
+### BEGIN INIT INFO
+# Short-Description: Fanfix service
+# Description: Starts the Fanfix remote service
+### END INIT INFO
+
+ENABLED=true
+USER=fanfix
+
+JAR=/path/to/fanfix.jar
+PINCODE="my password"
+PORT=12000
+
+FPID=/tmp/fanfix.pid
+OUT=/var/log/fanfix
+ERR=/var/log/fanfix.err
+
+if [ "$ENABLED" != true ]; then
+       [ "$1" != status ]
+       exit $?
+fi
+
+if [ ! -e "$JAR" ]; then
+       echo "Canot find main jar file: $JAR" >&2
+       exit 4
+fi
+
+case "$1" in
+start)
+       if sh "$0" status --quiet; then
+               echo "Fanfix is already running." >&2
+               false
+       else
+               sudo -u "$USER" -- java -jar "$JAR" --server "$PINCODE" "$PORT" > "$OUT" 2> "$ERR" &
+               echo $! > "$FPID"
+       fi
+       
+       sleep 0.1
+       sh "$0" status --quiet
+;;
+stop)
+       if sh "$0" status --quiet; then
+               sudo -u "$USER" -- java -jar "$JAR" --stop-server "$PINCODE" "$PORT"
+       fi
+       
+       i=1
+       while [ $i -lt 100 ]; do
+               if sh "$0" status --quiet; then
+                       echo -n . >&2
+                       sleep 1
+               fi
+               i=`expr $i + 1`
+       done
+       echo >&2
+       
+       if sh "$0" status --quiet; then
+               echo "Process not responding, killing it..." >&2
+               kill "`cat "$FPID"`"
+               sleep 10
+               kill -9 "`cat "$FPID"`" 2>/dev/null
+       fi
+       
+       rm "$FPID"
+;;
+restart)
+       sh "$0" stop
+       sh "$0" start
+;;
+status)
+       if [ -e "$FPID" ]; then
+               if [ "$2" = "--quiet" ]; then
+                       ps "`cat "$FPID"`" >/dev/null
+               else
+                       ps "`cat "$FPID"`" >/dev/null \
+                               && echo service is running >&2
+               fi
+       else
+               false
+       fi
+;;
+*)
+       echo $"Usage: $0 {start|stop|status|restart}" >&2
+       false
+;;
+esac
+