1#! /bin/sh
2#
3# Copyright (C) 2000-2015 Kern Sibbald
4# License: BSD 2-Clause; see file LICENSE-FOSS
5#
6# bacula       This shell script takes care of starting and stopping
7#	       the bacula daemons.
8#
9#   This is pretty much watered down version of the RedHat script
10#   that works on Solaris as well as Linux, but it won't work everywhere.
11#
12#   Submitted by Volker Sauer <volker@volker-sauer.de> 21Feb04
13#    Tweaked a bit by Kern to convert it to a .in file
14#
15# description: The Leading Open Source Backup Solution.
16#
17### BEGIN INIT INFO
18# Provides:		bacula
19# Required-Start:	network mysql
20# Required-Stop:
21# Default-Start:	3 5
22# Default-Stop:
23# Description:		run bacula daemon(s)
24### END INIT INFO
25
26PSCMD="@PSCMD@"
27PIDDIR=@piddir@
28SUBSYSDIR=@subsysdir@
29
30# A function to stop a program.
31killproc() {
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.
94pidofproc() {
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
128status() {
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 not running, 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 not running, but subsys locked"
167	return 2
168    fi
169    echo "$base is stopped"
170    return 3
171}
172
173success() {
174  return 0
175}
176
177failure() {
178  rc=$?
179  return $rc
180}
181
182case "$1" in
183    start)
184       echo "Starting the Storage daemon"
185       @sbindir@/bacula-sd $2 -v -c @sysconfdir@//bacula-sd.conf
186       echo "Starting the File daemon"
187       @sbindir@/bacula-fd $2 -v -c @sysconfdir@//bacula-fd.conf
188       sleep 2
189       echo "Starting the Director daemon"
190       @sbindir@/bacula-dir $2 -v -c @sysconfdir@//bacula-dir.conf
191       ;;
192    stop)
193       echo "Stopping the File daemon"
194       killproc @sbindir@/bacula-fd 9102
195       echo "Stopping the Storage daemon"
196       killproc @sbindir@/bacula-sd 9103
197       echo "Stopping the Director daemon"
198       killproc @sbindir@/bacula-dir 9101
199       echo
200       ;;
201    restart)
202       $0 stop
203       sleep 5
204       $0 start
205       ;;
206    status)
207       status @sbindir@/bacula-sd 9103
208       status @sbindir@/bacula-fd 9102
209       status @sbindir@/bacula-dir 9101
210       ;;
211    *)
212       echo "Usage: $0 {start|stop|restart|status}"
213       exit 1
214       ;;
215esac
216exit 0
217