Merge branch 'subtree'
[nikiroo-utils.git] / fanfix.sysv
1 #!/bin/sh
2 #
3 # fanfix This starts the Fanfix remote service.
4 #
5 # description: Starts the Fanfix remote service
6 #
7 ### BEGIN INIT INFO
8 # Default-Start: 3 4 5
9 # Short-Description: Fanfix service
10 # Description: Starts the Fanfix remote service
11 ### END INIT INFO
12
13 ENABLED=true
14 USER=fanfix
15 JAR=/path/to/fanfix.jar
16
17 FPID=/tmp/fanfix.pid
18 OUT=/var/log/fanfix
19 ERR=/var/log/fanfix.err
20
21 if [ "$ENABLED" != true ]; then
22 [ "$1" != status ]
23 exit $?
24 fi
25
26 if [ ! -e "$JAR" ]; then
27 echo "Canot find main jar file: $JAR" >&2
28 exit 4
29 fi
30
31 case "$1" in
32 start)
33 if sh "$0" status --quiet; then
34 echo "Fanfix is already running." >&2
35 false
36 else
37 [ -e "$OUT" ] && mv "$OUT" "$OUT".previous
38 [ -e "$ERR" ] && mv "$ERR" "$ERR".previous
39 sudo -u "$USER" -- java -jar "$JAR" --server > "$OUT" 2> "$ERR" &
40 echo $! > "$FPID"
41 fi
42
43 sleep 0.1
44 sh "$0" status --quiet
45 ;;
46 stop)
47 if sh "$0" status --quiet; then
48 sudo -u "$USER" -- java -jar "$JAR" --stop-server
49 fi
50
51 i=1
52 while [ $i -lt 100 ]; do
53 if sh "$0" status --quiet; then
54 echo -n . >&2
55 sleep 1
56 fi
57 i=`expr $i + 1`
58 done
59 echo >&2
60
61 if sh "$0" status --quiet; then
62 echo "Process not responding, killing it..." >&2
63 kill "`cat "$FPID"`"
64 sleep 10
65 kill -9 "`cat "$FPID"`" 2>/dev/null
66 fi
67
68 rm -f "$FPID"
69 ;;
70 restart)
71 sh "$0" stop
72 sh "$0" start
73 ;;
74 status)
75 if [ -e "$FPID" ]; then
76 if [ "$2" = "--quiet" ]; then
77 ps "`cat "$FPID"`" >/dev/null
78 else
79 ps "`cat "$FPID"`" >/dev/null \
80 && echo service is running >&2
81 fi
82 else
83 false
84 fi
85 ;;
86 *)
87 echo $"Usage: $0 {start|stop|status|restart}" >&2
88 false
89 ;;
90 esac
91