]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/bacula.in
f530ba66a376804064e08d6688111b240ddbeb85
[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 # A function to stop a program.
15 killproc() {
16     RC=0
17     # Test syntax.
18     if [ $# = 0 ]; then
19         echo "Usage: killproc {program} [signal]"
20         return 1
21     fi
22
23     notset=0
24     # check for third arg to be kill level
25     if [ "$3" != "" ] ; then
26         killlevel=$3
27     else
28         notset=1
29         killlevel="-9"
30     fi
31
32     # Get base program name
33     base=`basename $1`
34
35     # Find pid.
36     pid=`pidofproc $base $2`
37
38     # Kill it.
39     if [ "$pid" != "" ] ; then
40         if [ "$notset" = "1" ] ; then
41             if ps -p $pid>/dev/null 2>&1; then
42                 # TERM first, then KILL if not dead
43                 kill -TERM $pid 2>/dev/null
44                 sleep 1
45                 if ps -p $pid >/dev/null 2>&1 ; then
46                     sleep 1
47                     if ps -p $pid >/dev/null 2>&1 ; then
48                         sleep 3
49                         if ps -p $pid >/dev/null 2>&1 ; then
50                             kill -KILL $pid 2>/dev/null
51                         fi
52                     fi
53                 fi
54              fi
55              ps -p $pid >/dev/null 2>&1
56              RC=$?
57              [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
58         #    RC=$((! $RC))
59         # use specified level only
60         else
61             if ps -p $pid >/dev/null 2>&1; then
62                 kill $killlevel $pid 2>/dev/null
63                 RC=$?
64                 [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
65             fi
66         fi
67     else
68         failure "$base shutdown"
69     fi
70     # Remove pid file if any.
71     if [ "$notset" = "1" ]; then
72         rm -f @piddir@/$base.$2.pid
73     fi
74     return $RC
75 }
76
77 # A function to find the pid of a program.
78 pidofproc() {
79     pid=""
80     # Test syntax.
81     if [ $# = 0 ] ; then
82         echo "Usage: pidofproc {program}"
83         return 1
84     fi
85
86     # Get base program name
87     base=`basename $1`
88
89     # First try PID file
90     if [ -f @piddir@/$base.$2.pid ] ; then
91         pid=`head -1 @piddir@/$base.$2.pid`
92         if [ "$pid" != "" ] ; then
93             echo $pid
94             return 0
95         fi
96     fi
97
98     # Next try "pidof"
99    if [ -x /sbin/pidof ] ; then
100        pid=`/sbin/pidof $1`
101    fi
102    if [ "$pid" != "" ] ; then
103        echo $pid
104        return 0
105    fi
106
107     # Finally try to extract it from ps
108     ${PSCMD} | grep $1 | awk '{ print $1 }' | tr '\n' ' '
109     return 0
110 }
111
112 status() {
113     # Test syntax.
114     if [ $# = 0 ] ; then
115         echo "Usage: status {program}"
116         return 1
117     fi
118
119     # Get base program name
120     base=`basename $1`
121
122    # First try "pidof"
123    if [ -x /sbin/pidof ] ; then
124        pid=`/sbin/pidof $1`
125    fi
126    if [ "$pid" != "" ] ; then
127        echo "$base (pid $pid) is running..."
128        return 0
129    else
130        pid=`${PSCMD} | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
131              { if ((prog == $2) || (("(" prog ")") == $2) ||
132                   (("[" prog "]") == $2) ||
133                   ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
134        if [ "$pid" != "" ] ; then
135            echo "$base (pid $pid) is running..."
136            return 0
137        fi
138    fi
139
140     # Next try the PID files
141     if [ -f @piddir@/$base.$2.pid ] ; then
142         pid=`head -1 @piddir@/$base.$2.pid`
143         if [ "$pid" != "" ] ; then
144             echo "$base dead but pid file exists"
145             return 1
146         fi
147     fi
148     # See if the subsys lock exists
149     if [ -f @subsysdir@/$base ] ; then
150         echo "$base dead but subsys locked"
151         return 2
152     fi
153     echo "$base is stopped"
154     return 3
155 }
156
157 success() {
158   return 0
159 }
160
161 failure() {
162   rc=$?
163   return $rc
164 }
165
166 case "$1" in
167     start)
168        echo "Starting the Storage daemon"
169        @sbindir@/bacula-sd $2 -v -c @sysconfdir@/bacula-sd.conf
170        echo "Starting the File daemon"
171        @sbindir@/bacula-fd $2 -v -c @sysconfdir@/bacula-fd.conf
172        sleep 2
173        echo "Starting the Director daemon"
174        @sbindir@/bacula-dir $2 -v -c @sysconfdir@/bacula-dir.conf
175        ;;
176     stop)
177        echo "Stopping the File daemon"
178        killproc @sbindir@/bacula-fd @fd_port@
179        echo "Stopping the Storage daemon"
180        killproc @sbindir@/bacula-sd @sd_port@
181        echo "Stopping the Director daemon"
182        killproc @sbindir@/bacula-dir @dir_port@
183        echo
184        ;;
185     restart)
186        $0 stop
187        sleep 5
188        $0 start
189        ;;
190     status)
191        status @sbindir@/bacula-sd @sd_port@
192        status @sbindir@/bacula-fd @fd_port@
193        status @sbindir@/bacula-dir @dir_port@
194        ;;
195     *)
196        echo "Usage: $0 {start|stop|restart|status}"
197        exit 1
198        ;;
199 esac
200 exit 0