]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/bacula.in
Fix: clock diff, Dan's patch, Nic's patch, segfault
[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 BACBIN=@sbindir@
15 BACCFG=@sysconfdir@
16 PIDDIR=@piddir@
17 SUBSYSDIR=@subsysdir@
18
19 DIR_PORT=9101
20 FD_PORT=9102
21 SD_PORT=9103
22
23 DIR_USER=@dir_user@
24 DIR_GROUP=@dir_group@
25 FD_USER=@fd_user@
26 FD_GROUP=@fd_group@
27 SD_USER=@sd_user@
28 SD_GROUP=@sd_group@
29
30 # A function to stop a program.
31 killproc() {
32     RC=0
33     # Test syntax.
34     if [ $# = 0 ]; then
35         echo "Usage: killproc {program} [signal]"
36         return 1
37     fi
38
39     notset=0
40     # check for third arg to be kill level
41     if [ "$3" != "" ] ; then
42         killlevel=$3
43     else
44         notset=1
45         killlevel="-9"
46     fi
47
48     # Get base program name
49     base=`basename $1`
50
51     # Find pid.
52     pid=`pidofproc $base $2`
53
54     # Kill it.
55     if [ "$pid" != "" ] ; then
56         if [ "$notset" = "1" ] ; then
57             if ps -p $pid>/dev/null 2>&1; then
58                 # TERM first, then KILL if not dead
59                 kill -TERM $pid 2>/dev/null
60                 sleep 1
61                 if ps -p $pid >/dev/null 2>&1 ; then
62                     sleep 1
63                     if ps -p $pid >/dev/null 2>&1 ; then
64                         sleep 3
65                         if ps -p $pid >/dev/null 2>&1 ; then
66                             kill -KILL $pid 2>/dev/null
67                         fi
68                     fi
69                 fi
70              fi
71              ps -p $pid >/dev/null 2>&1
72              RC=$?
73              [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
74         #    RC=$((! $RC))
75         # use specified level only
76         else
77             if ps -p $pid >/dev/null 2>&1; then
78                 kill $killlevel $pid 2>/dev/null
79                 RC=$?
80                 [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
81             fi
82         fi
83     else
84         failure "$base shutdown"
85     fi
86     # Remove pid file if any.
87     if [ "$notset" = "1" ]; then
88         rm -f ${PIDDIR}/$base.$2.pid
89     fi
90     return $RC
91 }
92
93 # A function to find the pid of a program.
94 pidofproc() {
95     pid=""
96     # Test syntax.
97     if [ $# = 0 ] ; then
98         echo "Usage: pidofproc {program}"
99         return 1
100     fi
101
102     # Get base program name
103     base=`basename $1`
104
105     # First try PID file
106     if [ -f ${PIDDIR}/$base.$2.pid ] ; then
107         pid=`head -1 ${PIDDIR}/$base.$2.pid`
108         if [ "$pid" != "" ] ; then
109             echo $pid
110             return 0
111         fi
112     fi
113
114     # Next try "pidof"
115    if [ -x /sbin/pidof ] ; then
116        pid=`/sbin/pidof $1`
117    fi
118    if [ "$pid" != "" ] ; then
119        echo $pid
120        return 0
121    fi
122
123     # Finally try to extract it from ps
124     ${PSCMD} | grep $1 | awk '{ print $1 }' | tr '\n' ' '
125     return 0
126 }
127
128 status() {
129     # Test syntax.
130     if [ $# = 0 ] ; then
131         echo "Usage: status {program}"
132         return 1
133     fi
134
135     # Get base program name
136     base=`basename $1`
137
138    # First try "pidof"
139    if [ -x /sbin/pidof ] ; then
140        pid=`/sbin/pidof $1`
141    fi
142    if [ "$pid" != "" ] ; then
143        echo "$base (pid $pid) is running..."
144        return 0
145    else
146        pid=`${PSCMD} | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
147              { if ((prog == $2) || (("(" prog ")") == $2) ||
148                   (("[" prog "]") == $2) ||
149                   ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
150        if [ "$pid" != "" ] ; then
151            echo "$base (pid $pid) is running..."
152            return 0
153        fi
154    fi
155
156     # Next try the PID files
157     if [ -f ${PIDDIR}/$base.$2.pid ] ; then
158         pid=`head -1 ${PIDDIR}/$base.$2.pid`
159         if [ "$pid" != "" ] ; then
160             echo "$base dead but pid file exists"
161             return 1
162         fi
163     fi
164     # See if the subsys lock exists
165     if [ -f ${SUBSYSDIR}/$base ] ; then
166         echo "$base dead but subsys locked"
167         return 2
168     fi
169     echo "$base is stopped"
170     return 3
171 }
172
173 success() {
174   return 0
175 }
176
177 failure() {
178   rc=$?
179   return $rc
180 }
181
182 case "$1" in
183     start)
184        [ -x ${BACBIN}/bacula-sd ] && {
185           echo "Starting the Storage daemon"
186           OPTIONS=''
187           if [ "${SD_USER}" != '' ]; then
188              OPTIONS="${OPTIONS} -u ${SD_USER}"
189           fi
190
191           if [ "${SD_GROUP}" != '' ]; then
192              OPTIONS="${OPTIONS} -g ${SD_GROUP}"
193           fi
194
195           ${BACBIN}/bacula-sd $2 ${OPTIONS} -v -c ${BACCFG}/bacula-sd.conf
196        }
197
198        [ -x ${BACBIN}/bacula-fd ] && {
199           echo "Starting the File daemon"
200           OPTIONS=''
201           if [ "${FD_USER}" != '' ]; then
202              OPTIONS="${OPTIONS} -u ${FD_USER}"
203           fi
204
205           if [ "${FD_GROUP}" != '' ]; then
206              OPTIONS="${OPTIONS} -g ${FD_GROUP}"
207           fi
208
209           ${BACBIN}/bacula-fd $2 ${OPTIONS} -v -c ${BACCFG}/bacula-fd.conf
210        }
211
212        [ -x ${BACBIN}/bacula-dir ] && { 
213            sleep 2
214            echo "Starting the Director daemon"
215           OPTIONS=''
216           if [ "${DIR_USER}" != '' ]; then
217              OPTIONS="${OPTIONS} -u ${DIR_USER}"
218           fi
219
220           if [ "${DIR_GROUP}" != '' ]; then
221              OPTIONS="${OPTIONS} -g ${DIR_GROUP}"
222           fi
223
224           ${BACBIN}/bacula-dir $2 ${OPTIONS} -v -c ${BACCFG}/bacula-dir.conf
225        }
226        ;;
227
228     stop)
229        [ -x ${BACBIN}/bacula-sd ] && {
230           echo "Stopping the Storage daemon"
231           killproc ${BACBIN}/bacula-sd ${SD_PORT}
232        }
233
234        [ -x ${BACBIN}/bacula-fd ] && {
235           echo "Stopping the File daemon"
236           killproc ${BACBIN}/bacula-fd ${FD_PORT}
237        }
238
239        [ -x ${BACBIN}/bacula-dir ] && {
240           echo "Stopping the Director daemon"
241           killproc ${BACBIN}/bacula-dir ${DIR_PORT}
242        }
243        echo
244        ;;
245
246     restart)
247        $0 stop
248        sleep 5
249        $0 start
250        ;;
251
252     status)
253        [ -x ${BACBIN}/bacula-sd  ] && status ${BACBIN}/bacula-sd  ${SD_PORT}
254        [ -x ${BACBIN}/bacula-fd  ] && status ${BACBIN}/bacula-fd  ${FD_PORT}
255        [ -x ${BACBIN}/bacula-dir ] && status ${BACBIN}/bacula-dir ${DIR_PORT}
256        ;;
257
258     *)
259        echo "Usage: $0 {start|stop|restart|status}"
260        exit 1
261        ;;
262 esac
263 exit 0