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