-
Notifications
You must be signed in to change notification settings - Fork 256
/
bareos-ctl-funcs
executable file
·190 lines (164 loc) · 3.75 KB
/
bareos-ctl-funcs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! /bin/sh
# A function to find the pid of a program.
pidofproc() {
pid=""
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: pidofproc {program} {port}"
return 1
fi
# Get base program name
base=`basename $1`
# First try PID file
if [ -f ${PIDDIR}/$base.$2.pid ] ; then
pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi
# Next try "pidof"
if [ ! -z "${PIDOF}" -a -x "${PIDOF}" ] ; then
pid=`${PIDOF} $1`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi
# Next try "pgrep"
if [ ! -z "${PGREP}" -a -x "${PGREP}" ] ; then
pid=`${PGREP} -f $base`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi
# Finally try to extract it from ps
pid=`${PSCMD} | grep $base | ${AWK} '{ print $1 }' | tr '\n' ' '`
echo $pid
return 0
}
checkPid()
{
pid="$1"
wait="${2:-0}"
waittext="${3:-}"
output=""
if [ -z "$pid" ]; then
return 1
fi
start=$(date +%s)
end=$(expr $start + $wait)
while [ "$(date +%s)" -lt "$end" ] && ${PS} -p "$pid" >/dev/null 2>&1; do
if [ "${waittext}" ]; then
printf "%s" "$waittext"
output="yes"
fi
# not all systems offer floating point numbers as sleep parameter, therefore try both.
sleep 0.1 || sleep 1
done
if [ "${output}" ]; then printf " "; fi
${PS} -p "$pid" >/dev/null 2>&1
return $?
}
# A function to stop a program.
killproc() {
RC=0
# Test syntax.
if [ $# = 0 ]; then
echo "Usage: killproc {program} {port} [signal]"
return 1
fi
notset=0
# check for third arg to be kill level
if [ "$3" != "" ] ; then
killlevel=$3
else
notset=1
killlevel="-9"
fi
# Get base program name
base=`basename $1`
# Find pid.
pid=`pidofproc $base $2`
# Kill it.
if checkPid "$pid"; then
if [ "$notset" = "1" ] ; then
# TERM first, then KILL if not dead
kill -TERM $pid 2>/dev/null
if checkPid "$pid" 3 "."; then
kill -KILL $pid 2>/dev/null
fi
if checkPid "$pid" 3 "_"; then
failure
else
success
fi
else
if kill $killlevel $pid 2>/dev/null; then
success "sent signal $killlevel"
else
failure
fi
fi
else
success "already stopped"
fi
# Remove pid file if any.
if [ "$notset" = "1" ]; then
rm -f "${PIDDIR}/$base.$2.pid"
fi
return $RC
}
# Function that checks if the configuration is OK and the Database can be connected
checkcfg() {
RC=0
echo "Checking Configuration and Database connection ... "
if [ ! -z "${DIR_USER}" -a "${DIR_USER}" != "${USER}" ]; then
su -s /bin/sh "${DIR_USER}" -c "${BAREOS_DIRECTOR_BINARY}" ${OPTIONS} -f -t
RC=$?
else
"${BAREOS_DIRECTOR_BINARY}" ${OPTIONS} -f -t
RC=$?
fi
return $RC
}
status() {
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: status {program} {port}"
return 1
fi
# Get base program name
base=`basename $1`
# Find pid.
pid=`pidofproc $base $2`
if checkPid "$pid"; then
echo "$base (pid $pid) is running..."
return 0
fi
if [ -f ${PIDDIR}/$base.$2.pid ] ; then
echo "$base dead but pid file exists"
return 1
fi
# See if the subsys lock exists
if [ -f ${SUBSYSDIR}/$base ] ; then
echo "$base dead but subsys locked"
return 2
fi
echo "$base is stopped"
return 3
}
success() {
MESSAGE="OK"
if [ "$1" ]; then
MESSAGE="OK ($1)"
fi
echo $MESSAGE
return 0
}
failure() {
rc=$?
echo "failed (rc=$rc)"
return $rc
}