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