]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/bacula-ctl-fd.in
Backport from Bacula Enterprise
[bacula/bacula] / bacula / scripts / bacula-ctl-fd.in
1 #! /bin/sh
2 #
3 #   Bacula(R) - The Network Backup Solution
4 #
5 #   Copyright (C) 2000-2015 Kern Sibbald
6 #   Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
7 #
8 #   The original author of Bacula is Kern Sibbald, with contributions
9 #   from many others, a complete list can be found in the file AUTHORS.
10 #
11 #   You may use this file and others of this release according to the
12 #   license defined in the LICENSE file, which includes the Affero General
13 #   Public License, v3.0 ("AGPLv3") and some additional permissions and
14 #   terms pursuant to its AGPLv3 Section 7.
15 #
16 #   This notice must be preserved when any source code is 
17 #   conveyed and/or propagated.
18 #
19 #   Bacula(R) is a registered trademark of Kern Sibbald.
20 #
21 # bacula-ctl-fd This shell script takes care of starting and stopping
22 #               the bacula File daemon.
23 #
24 #   This is pretty much watered down version of the RedHat script
25 #   that works on Solaris as well as Linux, but it won't work everywhere.
26 #
27 # description: The Leading Open Source Backup Solution.
28 #
29
30 PSCMD="@PSCMD@"
31 PS="ps"
32
33 #
34 # On Solaris, you may need to use nawk, or alternatively,
35 #  add the GNU binaries to your path, such as /usr/xpg4/bin
36 #
37 AWK=@AWK@
38
39 # All these are not *really* needed but it makes it
40 #  easier to "steal" this code for the development 
41 #  environment where they are different.
42 #  
43 BACFDBIN=@sbindir@
44 BACFDCFG=@sysconfdir@
45 PIDDIR=@piddir@
46 SUBSYSDIR=@subsysdir@
47
48 FD_PORT=@fd_port@
49
50 FD_USER=@fd_user@
51 FD_GROUP=@fd_group@
52 Bacula="@BACULA@"
53 PIDOF=@PIDOF@   
54
55 # A function to stop a program.
56 killproc() {
57    RC=0
58    # Test syntax.
59    if [ $# = 0 ]; then
60       echo "Usage: killproc {program} {port} [signal]"
61       return 1
62    fi
63
64    notset=0
65    # check for third arg to be kill level
66    if [ "$3" != "" ] ; then
67       killlevel=$3
68    else
69       notset=1
70       killlevel="-9"
71    fi
72
73    # Get base program name
74    base=`basename $1`
75
76    # Find pid.
77    pid=`pidofproc $base $2`
78
79    # Kill it.
80    if [ "$pid" != "" ] ; then
81       if [ "$notset" = "1" ] ; then
82          if ${PS} -p "$pid">/dev/null 2>&1; then
83              # TERM first, then KILL if not dead
84              kill -TERM $pid 2>/dev/null
85              sleep 1
86              if ${PS} -p "$pid" >/dev/null 2>&1 ; then
87                  sleep 1
88                  if ${PS} -p "$pid" >/dev/null 2>&1 ; then
89                      sleep 3
90                      if ${PS} -p "$pid" >/dev/null 2>&1 ; then
91                          kill -KILL $pid 2>/dev/null
92                      fi
93                  fi
94              fi
95           fi
96           ${PS} -p "$pid" >/dev/null 2>&1
97           RC=$?
98           [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
99       #    RC=$((! $RC))
100       # use specified level only
101       else
102          if ${PS} -p "$pid" >/dev/null 2>&1; then
103             kill $killlevel $pid 2>/dev/null
104             RC=$?
105             [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
106          fi
107       fi
108    else
109       failure "$base shutdown"
110    fi
111    # Remove pid file if any.
112    if [ "$notset" = "1" ]; then
113       rm -f ${PIDDIR}/$base.$2.pid
114    fi
115    return $RC
116 }
117
118 # A function to find the pid of a program.
119 pidofproc() {
120    pid=""
121    # Test syntax.
122    if [ $# = 0 ] ; then
123       echo "Usage: pidofproc {program}"
124       return 1
125    fi
126
127    # Get base program name
128    base=`basename $1`
129
130    # First try PID file
131    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
132       pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
133       if [ "$pid" != "" ] ; then
134          echo $pid
135          return 0
136       fi
137    fi
138
139    # Next try "pidof"
140    if [ -x ${PIDOF} ] ; then
141       pid=`${PIDOF} $1`
142    fi
143    if [ "$pid" != "" ] ; then
144       echo $pid
145       return 0
146    fi
147
148    # Finally try to extract it from ps
149    pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
150    echo $pid
151    return 0
152 }
153
154 status() {
155    pid=""
156    # Test syntax.
157    if [ $# = 0 ] ; then
158        echo "Usage: status {program} {port}"
159        return 1
160    fi
161
162    # Get base program name
163    base=`basename $1`
164
165    # First try "pidof"
166    if [ -x ${PIDOF} ] ; then
167       pid=`${PIDOF} $1`
168    fi
169    if [ "$pid" != "" ] ; then
170       echo "$base (pid $pid) is running..."
171       return 0
172    else
173       pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 } 
174             { if ((prog == $2) || (("(" prog ")") == $2) ||
175                  (("[" prog "]") == $2) ||
176                  ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
177       if [ "$pid" != "" ] ; then
178          echo "$base (pid $pid) is running..."
179          return 0
180       fi
181    fi
182
183    # Next try the PID files
184    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
185       pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
186       if [ "$pid" != "" ] ; then
187          echo "$base dead but pid file exists"
188          return 1
189       fi
190    fi
191    # See if the subsys lock exists
192    if [ -f ${SUBSYSDIR}/$base ] ; then
193       echo "$base dead but subsys locked"
194       return 2
195    fi
196    echo "$base is stopped"
197    return 3
198 }
199
200 success() {
201    return 0
202 }
203
204 failure() {
205    rc=$?
206    return $rc
207 }
208
209 OS=`uname -s`
210
211 # if /lib/tls exists, force Bacula to use the glibc pthreads instead
212 if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
213    export LD_ASSUME_KERNEL=2.4.19
214 fi
215
216 case "$1" in
217    start)
218       [ -x ${BACFDBIN}/bacula-fd ] && {
219          echo "Starting the $Bacula File daemon"
220          OPTIONS=''
221          if [ "${FD_USER}" != '' ]; then
222             OPTIONS="${OPTIONS} -u ${FD_USER}"
223          fi
224
225          if [ "${FD_GROUP}" != '' ]; then
226             OPTIONS="${OPTIONS} -g ${FD_GROUP}"
227          fi
228
229          if [ "x${VALGRIND_FD}" = "x1" ]; then
230             valgrind --leak-check=full ${BACFDBIN}/bacula-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
231          else
232             ${BACFDBIN}/bacula-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
233          fi
234       }
235       ;;
236
237    stop)
238       # Stop the FD first so that SD will fail jobs and update catalog
239       [ -x ${BACFDBIN}/bacula-fd ] && {
240          echo "Stopping the $Bacula File daemon"
241          killproc ${BACFDBIN}/bacula-fd ${FD_PORT} $2
242       }
243       ;;
244
245    restart)
246       $0 stop
247       sleep 5
248       $0 start
249       ;;
250
251    status)
252       [ -x ${BACFDBIN}/bacula-fd ] && status ${BACFDBIN}/bacula-fd  ${FD_PORT}
253       ;;
254
255    *)
256       echo "Usage: $0 {start|stop|restart|status}"
257       exit 1
258       ;;
259 esac
260 exit 0