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