add 'devstart' command for init script
[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             killed=0
34             pid_old="$(cat "${PPATH}/brisk.pid")"
35             sig="TERM"
36             for i in $(seq 1 $WAITLOOP_MAX); do
37                 sleep 1
38                 if ! kill -$sig $pid_old 2>/dev/null ; then
39                     killed=1
40                     break
41                 fi
42                 sig=0
43             done
44             if [ $killed -eq 0 ]; then
45                 kill -KILL $pid_old 2>/dev/null || true
46             fi
47         fi
48         ;;
49
50     devstart)
51         su - ${BUSER} -c 'cd '"$BPATH"'/spush ; ./brisk-spush.php'
52         ;;
53
54     start)
55         su - ${BUSER} -c 'cd '"$BPATH"'/spush ; screen -d -m -S '"${SSUFF}"' bash -c '"'"'while [ 1 ]; do cd . ; ./brisk-spush.php \| grep "IN LOOP" ; if [ $? -eq 0 ]; then break ; fi ; done'"'"
56         ;;
57     restart)
58         $0 stop
59         sleep 3
60         $0 start
61         ;;
62     *)
63         echo "Usage: $0 {start|stop|restart}" >&2
64         exit 1
65         ;;
66 esac