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