]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/bacula-ctl-dir.in
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / scripts / bacula-ctl-dir.in
1 #! /bin/sh
2 #
3 # bacula-ctl-dir This shell script takes care of starting and stopping
4 #                the bacula Director daemon
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 BACDIRBIN=@sbindir@
26 BACDIRCFG=@sysconfdir@
27 PIDDIR=@piddir@
28 SUBSYSDIR=@subsysdir@
29
30 DIR_PORT=@dir_port@
31
32 DIR_USER=@dir_user@
33 DIR_GROUP=@dir_group@
34
35 PIDOF=@PIDOF@   
36
37 # A function to stop a program.
38 killproc() {
39    RC=0
40    # Test syntax.
41    if [ $# = 0 ]; then
42       echo "Usage: killproc {program} {port} [signal]"
43       return 1
44    fi
45
46    notset=0
47    # check for third arg to be kill level
48    if [ "$3" != "" ] ; then
49       killlevel=$3
50    else
51       notset=1
52       killlevel="-9"
53    fi
54
55    # Get base program name
56    base=`basename $1`
57
58    # Find pid.
59    pid=`pidofproc $base $2`
60
61    # Kill it.
62    if [ "$pid" != "" ] ; then
63       if [ "$notset" = "1" ] ; then
64          if ${PS} -p "$pid">/dev/null 2>&1; then
65              # TERM first, then KILL if not dead
66              kill -TERM $pid 2>/dev/null
67              sleep 1
68              if ${PS} -p "$pid" >/dev/null 2>&1 ; then
69                  sleep 1
70                  if ${PS} -p "$pid" >/dev/null 2>&1 ; then
71                      sleep 3
72                      if ${PS} -p "$pid" >/dev/null 2>&1 ; then
73                          kill -KILL $pid 2>/dev/null
74                      fi
75                  fi
76              fi
77           fi
78           ${PS} -p "$pid" >/dev/null 2>&1
79           RC=$?
80           [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
81       #    RC=$((! $RC))
82       # use specified level only
83       else
84          if ${PS} -p "$pid" >/dev/null 2>&1; then
85             kill $killlevel $pid 2>/dev/null
86             RC=$?
87             [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
88          fi
89       fi
90    else
91       failure "$base shutdown"
92    fi
93    # Remove pid file if any.
94    if [ "$notset" = "1" ]; then
95       rm -f ${PIDDIR}/$base.$2.pid
96    fi
97    return $RC
98 }
99
100 # A function to find the pid of a program.
101 pidofproc() {
102    pid=""
103    # Test syntax.
104    if [ $# = 0 ] ; then
105       echo "Usage: pidofproc {program}"
106       return 1
107    fi
108
109    # Get base program name
110    base=`basename $1`
111
112    # First try PID file
113    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
114       pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
115       if [ "$pid" != "" ] ; then
116          echo $pid
117          return 0
118       fi
119    fi
120
121    # Next try "pidof"
122    if [ -x ${PIDOF} ] ; then
123       pid=`${PIDOF} $1`
124    fi
125    if [ "$pid" != "" ] ; then
126       echo $pid
127       return 0
128    fi
129
130    # Finally try to extract it from ps
131    ${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '
132    return 0
133 }
134
135 status() {
136    pid=""
137    # Test syntax.
138    if [ $# = 0 ] ; then
139        echo "Usage: status {program} {port}"
140        return 1
141    fi
142
143    # Get base program name
144    base=`basename $1`
145
146    # First try "pidof"
147    if [ -x ${PIDOF} ] ; then
148       pid=`${PIDOF} $1`
149    fi
150    if [ "$pid" != "" ] ; then
151       echo "$base (pid $pid) is running..."
152       return 0
153    else
154       pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 } 
155             { if ((prog == $2) || (("(" prog ")") == $2) ||
156                  (("[" prog "]") == $2) ||
157                  ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
158       if [ "$pid" != "" ] ; then
159          echo "$base (pid $pid) is running..."
160          return 0
161       fi
162    fi
163
164    # Next try the PID files
165    if [ -f ${PIDDIR}/$base.$2.pid ] ; then
166       pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
167       if [ "$pid" != "" ] ; then
168          echo "$base dead but pid file exists"
169          return 1
170       fi
171    fi
172    # See if the subsys lock exists
173    if [ -f ${SUBSYSDIR}/$base ] ; then
174       echo "$base dead but subsys locked"
175       return 2
176    fi
177    echo "$base is stopped"
178    return 3
179 }
180
181 success() {
182    return 0
183 }
184
185 failure() {
186    rc=$?
187    return $rc
188 }
189
190 OS=`uname -s`
191
192 # if /lib/tls exists, force Bacula to use the glibc pthreads instead
193 if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
194    export LD_ASSUME_KERNEL=2.4.19
195 fi
196
197 case "$1" in
198    start)
199       [ -x ${BACDIRBIN}/bacula-dir ] && { 
200           sleep 2
201           echo "Starting the Bacula Director daemon"
202          OPTIONS=''
203          if [ "${DIR_USER}" != '' ]; then
204             OPTIONS="${OPTIONS} -u ${DIR_USER}"
205          fi
206
207          if [ "${DIR_GROUP}" != '' ]; then
208             OPTIONS="${OPTIONS} -g ${DIR_GROUP}"
209          fi
210
211          ${BACDIRBIN}/bacula-dir $2 ${OPTIONS} -v -c ${BACDIRCFG}/bacula-dir.conf
212       }
213       ;;
214
215    stop)
216       [ -x ${BACDIRBIN}/bacula-dir ] && {
217          echo "Stopping the Bacula Director daemon"
218          killproc ${BACDIRBIN}/bacula-dir ${DIR_PORT}
219       }
220       ;;
221
222    restart)
223       $0 stop
224       sleep 5
225       $0 start
226       ;;
227
228    status)
229       [ -x ${BACDIRBIN}/bacula-dir ] && status ${BACDIRBIN}/bacula-dir ${DIR_PORT}
230       ;;
231
232    *)
233       echo "Usage: $0 {start|stop|restart|status}"
234       exit 1
235       ;;
236 esac
237 exit 0