]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/bacula.in
Another fix from Ludovic
[bacula/bacula] / bacula / scripts / bacula.in
1 #! /bin/sh
2 #
3 # bacula       This shell script takes care of starting and stopping
4 #              the bacula daemons.
5 #
6 #   This is pretty much watered down version of the RedHat script
7 #   that works on Solaris as well as Linux, but it won't work everywhere.
8 #
9 # description: It comes by night and sucks the vital essence from your computers.
10 #
11
12 PSCMD="@PSCMD@"
13 PS="ps"
14
15 #
16 # On Solaris, you may need to use nawk, or alternatively,
17 #  add the GNU binaries to your path, such as /usr/xpg4/bin
18 #
19 AWK=@AWK@
20
21 # All these are not *really* needed but it makes it
22 #  easier to "steal" this code for the development 
23 #  environment where they are different.
24 #  
25 BACFDBIN=@sbindir@
26 BACFDCFG=@sysconfdir@
27 BACSDBIN=@sbindir@
28 BACSDCFG=@sysconfdir@
29 BACDIRBIN=@sbindir@
30 BACDIRCFG=@sysconfdir@
31 PIDDIR=@piddir@
32 SUBSYSDIR=@subsysdir@
33
34 DIR_PORT=@dir_port@
35 FD_PORT=@fd_port@
36 SD_PORT=@sd_port@
37
38 DIR_USER=@dir_user@
39 DIR_GROUP=@dir_group@
40 FD_USER=@fd_user@
41 FD_GROUP=@fd_group@
42 SD_USER=@sd_user@
43 SD_GROUP=@sd_group@
44
45 PIDOF=@PIDOF@   
46
47 # A function to stop a program.
48 killproc() {
49    RC=0
50    # Test syntax.
51    if [ $# = 0 ]; then
52       echo "Usage: killproc {program} {port} [signal]"
53       return 1
54    fi
55
56    notset=0
57    # check for third arg to be kill level
58    if [ "$3" != "" ] ; then
59       killlevel=$3
60    else
61       notset=1
62       killlevel="-9"
63    fi
64
65    # Get base program name
66    base=`basename $1`
67
68    # Find pid.
69    pid=`pidofproc $base $2`
70
71    # Kill it.
72    if [ "$pid" != "" ] ; then
73       if [ "$notset" = "1" ] ; then
74          if ${PS} -p $pid>/dev/null 2>&1; then
75              # TERM first, then KILL if not dead
76              kill -TERM $pid 2>/dev/null
77              sleep 1
78              if ${PS} -p $pid >/dev/null 2>&1 ; then
79                  sleep 1
80                  if ${PS} -p $pid >/dev/null 2>&1 ; then
81                      sleep 3
82                      if ${PS} -p $pid >/dev/null 2>&1 ; then
83                          kill -KILL $pid 2>/dev/null
84                      fi
85                  fi
86              fi
87           fi
88           ${PS} -p $pid >/dev/null 2>&1
89           RC=$?
90           [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
91       #    RC=$((! $RC))
92       # use specified level only
93       else
94          if ${PS} -p $pid >/dev/null 2>&1; then
95             kill $killlevel $pid 2>/dev/null
96             RC=$?
97             [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
98          fi
99       fi
100    else
101       failure "$base shutdown"
102    fi
103    # Remove pid file if any.
104    if [ "$notset" = "1" ]; then
105       rm -f ${PIDDIR}/$base.$2.pid
106    fi
107    return $RC
108 }
109
110 # A function to find the pid of a program.
111 pidofproc() {
112    pid=""
113    # Test syntax.
114    if [ $# = 0 ] ; then
115       echo "Usage: pidofproc {program}"
116       return 1
117    fi
118
119    # Get base program name
120    base=`basename $1`
121
122    # First try PID file
123    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
124       pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
125       if [ "$pid" != "" ] ; then
126          echo $pid
127          return 0
128       fi
129    fi
130
131    # Next try "pidof"
132    if [ -x ${PIDOF} ] ; then
133       pid=`${PIDOF} $1`
134    fi
135    if [ "$pid" != "" ] ; then
136       echo $pid
137       return 0
138    fi
139
140    # Finally try to extract it from ps
141    ${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '
142    return 0
143 }
144
145 status() {
146    pid=""
147    # Test syntax.
148    if [ $# = 0 ] ; then
149        echo "Usage: status {program} {port}"
150        return 1
151    fi
152
153    # Get base program name
154    base=`basename $1`
155
156    # First try "pidof"
157    if [ -x ${PIDOF} ] ; then
158       pid=`${PIDOF} $1`
159    fi
160    if [ "$pid" != "" ] ; then
161       echo "$base (pid $pid) is running..."
162       return 0
163    else
164       pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 } 
165             { if ((prog == $2) || (("(" prog ")") == $2) ||
166                  (("[" prog "]") == $2) ||
167                  ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
168       if [ "$pid" != "" ] ; then
169          echo "$base (pid $pid) is running..."
170          return 0
171       fi
172    fi
173
174    # Next try the PID files
175    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
176       pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
177       if [ "$pid" != "" ] ; then
178          echo "$base dead but pid file exists"
179          return 1
180       fi
181    fi
182    # See if the subsys lock exists
183    if [ -f ${SUBSYSDIR}/$base ] ; then
184       echo "$base dead but subsys locked"
185       return 2
186    fi
187    echo "$base is stopped"
188    return 3
189 }
190
191 success() {
192    return 0
193 }
194
195 failure() {
196    rc=$?
197    return $rc
198 }
199
200 OS=`uname -s`
201
202 # if /lib/tls exists, force Bacula to use the glibc pthreads instead
203 if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
204    export LD_ASSUME_KERNEL=2.4.19
205 fi
206
207 case "$1" in
208    start)
209       [ -x ${BACSDBIN}/bacula-sd ] && {
210          echo "Starting the Bacula Storage daemon"
211          OPTIONS=''
212          if [ "${SD_USER}" != '' ]; then
213             OPTIONS="${OPTIONS} -u ${SD_USER}"
214          fi
215
216          if [ "${SD_GROUP}" != '' ]; then
217             OPTIONS="${OPTIONS} -g ${SD_GROUP}"
218          fi
219
220          ${BACSDBIN}/bacula-sd $2 ${OPTIONS} -v -c ${BACSDCFG}/bacula-sd.conf
221       }
222
223       [ -x ${BACFDBIN}/bacula-fd ] && {
224          echo "Starting the Bacula File daemon"
225          OPTIONS=''
226          if [ "${FD_USER}" != '' ]; then
227             OPTIONS="${OPTIONS} -u ${FD_USER}"
228          fi
229
230          if [ "${FD_GROUP}" != '' ]; then
231             OPTIONS="${OPTIONS} -g ${FD_GROUP}"
232          fi
233
234          ${BACFDBIN}/bacula-fd $2 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
235       }
236
237       [ -x ${BACDIRBIN}/bacula-dir ] && { 
238           sleep 2
239           echo "Starting the Bacula Director daemon"
240          OPTIONS=''
241          if [ "${DIR_USER}" != '' ]; then
242             OPTIONS="${OPTIONS} -u ${DIR_USER}"
243          fi
244
245          if [ "${DIR_GROUP}" != '' ]; then
246             OPTIONS="${OPTIONS} -g ${DIR_GROUP}"
247          fi
248
249          ${BACDIRBIN}/bacula-dir $2 ${OPTIONS} -v -c ${BACDIRCFG}/bacula-dir.conf
250       }
251       ;;
252
253    stop)
254       # Stop the FD first so that SD will fail jobs and update catalog
255       [ -x ${BACFDBIN}/bacula-fd ] && {
256          echo "Stopping the Bacula File daemon"
257          killproc ${BACFDBIN}/bacula-fd ${FD_PORT}
258       }
259
260       [ -x ${BACSDBIN}/bacula-sd ] && {
261          echo "Stopping the Bacula Storage daemon"
262          killproc ${BACSDBIN}/bacula-sd ${SD_PORT}
263       }
264
265       [ -x ${BACDIRBIN}/bacula-dir ] && {
266          echo "Stopping the Bacula Director daemon"
267          killproc ${BACDIRBIN}/bacula-dir ${DIR_PORT}
268       }
269       echo
270       ;;
271
272    restart)
273       $0 stop
274       sleep 5
275       $0 start
276       ;;
277
278    status)
279       [ -x ${BACSDBIN}/bacula-sd   ] && status ${BACSDBIN}/bacula-sd  ${SD_PORT}
280       [ -x ${BACFDBIN}/bacula-fd   ] && status ${BACFDBIN}/bacula-fd  ${FD_PORT}
281       [ -x ${BACDIRBIN}/bacula-dir ] && status ${BACDIRBIN}/bacula-dir ${DIR_PORT}
282       ;;
283
284    *)
285       echo "Usage: $0 {start|stop|restart|status}"
286       exit 1
287       ;;
288 esac
289 exit 0