init script installation added and version updated
[brisk.git] / bin / brisk-init.sh
1 #!/bin/sh -e
2 ### BEGIN INIT INFO
3 # Provides:          brisk
4 # Required-Start:       $local_fs $remote_fs $network $time
5 # Required-Stop:        $local_fs $remote_fs $network $time
6 # Default-Start:        2 3 4 5
7 # Default-Stop:         0 1 6
8 # Short-Description: manage brisk daemon
9 ### END INIT INFO
10
11 BPATH="xx/home/nastasi/web/brisk"
12 PPATH="xx/home/nastasi/brisk-priv"
13 # screen suffix
14 SSUFF="xxbrisk"
15 BUSER="xxwww-data"
16 # seconds to wait exit of the process
17 WAITLOOP_MAX=5
18
19 #
20 #  MAIN
21 #
22 NL="
23 "
24 TB="    "
25 # scr_old="$(screen -ls | sed "s/^[ ${TB}]*//g;s/[ ${TB}]\+/ /g" | cut -d ' ' -f 1 | grep "\.${SSUFF}$")"
26 # echo "[$scr_old]"
27
28 case "$1" in
29     stop)
30         #
31         #  if .pid file exists try to shutdown the process
32         if [ -f "${PPATH}/brisk.pid" ]; then
33             pid_old="$(cat "${PPATH}/brisk.pid")"
34             kill -TERM $pid_old
35             killed=0
36             for i in $(seq 1 $WAITLOOP_MAX); do
37                 sleep 1
38                 if ! kill -0 $pid_old 2>/dev/null ; then
39                     killed=1
40                     break
41                 fi
42             done
43             if [ $killed -eq 0 ]; then
44                 kill -KILL $pid_old 2>/dev/null
45             fi
46         fi
47         ;;
48
49     start)
50         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'"'"
51         ;;
52     restart)
53         $0 stop
54         sleep 3
55         $0 start
56         ;;
57     *)
58         echo "Usage: $0 {start|stop|restart}" >&2
59         exit 1
60         ;;
61 esac