8784: Fix test for latest firefox.
[arvados.git] / services / keepproxy / pkg-extras / etc / init.d / keepproxy
1 #!/bin/sh
2 # Init script for keepproxy
3 # Maintained by 
4 # Generated by pleaserun.
5 # Implemented based on LSB Core 3.1:
6 #   * Sections: 20.2, 20.3
7 #
8 ### BEGIN INIT INFO
9 # Provides:          keepproxy
10 # Required-Start:    $remote_fs $syslog
11 # Required-Stop:     $remote_fs $syslog
12 # Default-Start:     2 3 4 5
13 # Default-Stop:      0 1 6
14 # Short-Description: 
15 # Description:       no description given
16 ### END INIT INFO
17
18 PATH=/sbin:/usr/sbin:/bin:/usr/bin
19 export PATH
20
21 name=keepproxy
22 program=/usr/bin/keepproxy
23 args=''
24 pidfile="/var/run/$name.pid"
25
26 [ -r /etc/default/$name ] && . /etc/default/$name
27 [ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name
28
29 trace() {
30   logger -t "/etc/init.d/keepproxy" "$@"
31 }
32
33 emit() {
34   trace "$@"
35   echo "$@"
36 }
37
38 start() {
39
40   # Ensure the log directory is setup correctly.
41   [ ! -d "/var/log/" ] && mkdir "/var/log/"
42   chown "$user":"$group" "/var/log/"
43   chmod 755 "/var/log/"
44
45
46   # Setup any environmental stuff beforehand
47   
48
49   # Run the program!
50   
51   chroot --userspec "$user":"$group" "$chroot" sh -c "
52     
53     cd \"$chdir\"
54     exec \"$program\" $args
55   " >> /var/log/keepproxy.stdout 2>> /var/log/keepproxy.stderr &
56
57   # Generate the pidfile from here. If we instead made the forked process
58   # generate it there will be a race condition between the pidfile writing
59   # and a process possibly asking for status.
60   echo $! > $pidfile
61
62   emit "$name started"
63   return 0
64 }
65
66 stop() {
67   # Try a few times to kill TERM the program
68   if status ; then
69     pid=$(cat "$pidfile")
70     trace "Killing $name (pid $pid) with SIGTERM"
71     kill -TERM $pid
72     # Wait for it to exit.
73     for i in 1 2 3 4 5 ; do
74       trace "Waiting $name (pid $pid) to die..."
75       status || break
76       sleep 1
77     done
78     if status ; then
79       emit "$name stop failed; still running."
80     else
81       emit "$name stopped."
82     fi
83   fi
84 }
85
86 status() {
87   if [ -f "$pidfile" ] ; then
88     pid=$(cat "$pidfile")
89     if ps -p $pid > /dev/null 2> /dev/null ; then
90       # process by this pid is running.
91       # It may not be our pid, but that's what you get with just pidfiles.
92       # TODO(sissel): Check if this process seems to be the same as the one we
93       # expect. It'd be nice to use flock here, but flock uses fork, not exec,
94       # so it makes it quite awkward to use in this case.
95       return 0
96     else
97       return 2 # program is dead but pid file exists
98     fi
99   else
100     return 3 # program is not running
101   fi
102 }
103
104 force_stop() {
105   if status ; then
106     stop
107     status && kill -KILL $(cat "$pidfile")
108   fi
109 }
110
111
112 case "$1" in
113   force-start|start|stop|force-stop|restart)
114     trace "Attempting '$1' on keepproxy"
115     ;;
116 esac
117
118 case "$1" in
119   force-start)
120     PRESTART=no
121     exec "$0" start
122     ;;
123   start)
124     status
125     code=$?
126     if [ $code -eq 0 ]; then
127       emit "$name is already running"
128       exit $code
129     else
130       start
131       exit $?
132     fi
133     ;;
134   stop) stop ;;
135   force-stop) force_stop ;;
136   status) 
137     status
138     code=$?
139     if [ $code -eq 0 ] ; then
140       emit "$name is running"
141     else
142       emit "$name is not running"
143     fi
144     exit $code
145     ;;
146   restart) 
147     
148     stop && start 
149     ;;
150   *)
151     echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2
152     exit 3
153   ;;
154 esac
155
156 exit $?