[6370fa6] | 1 | <sect1 id="ch07-functions">
|
---|
| 2 | <title>Creating the functions script</title>
|
---|
| 3 |
|
---|
| 4 | <para>
|
---|
| 5 | Create a new file <filename>/etc/init.d/functions</filename> containing
|
---|
| 6 | the following:
|
---|
| 7 | </para>
|
---|
| 8 |
|
---|
| 9 | <literallayout>
|
---|
| 10 |
|
---|
| 11 | <userinput>cat > functions << "EOF"</userinput>
|
---|
| 12 | #!/bin/sh
|
---|
| 13 | # Begin /etc/init.d/functions
|
---|
| 14 |
|
---|
| 15 | #
|
---|
| 16 | # Set a few variables that influence the text that's printed on the
|
---|
| 17 | # screen. The SET_COL variable starts the text in column number 70 (as
|
---|
| 18 | # defined by the COL variable). NORMAL prints text in normal mode.
|
---|
| 19 | # SUCCESS prints text in a green colour and FAILURE prints text in a red
|
---|
| 20 | # colour
|
---|
| 21 | #
|
---|
| 22 |
|
---|
| 23 | COL=70
|
---|
| 24 | SET_COL="echo -en \\033[${COL}G"
|
---|
| 25 | NORMAL="echo -en \\033[0;39m"
|
---|
| 26 | SUCCESS="echo -en \\033[1;32m"
|
---|
| 27 | FAILURE="echo -en \\033[1;31m"
|
---|
| 28 |
|
---|
| 29 | #
|
---|
| 30 | # The evaluate_retval function evaluates the return value of the process
|
---|
| 31 | # that was run just before this function was called. If the return value
|
---|
| 32 | # was 0, indicating success, the print_status function is called with
|
---|
| 33 | # the 'success' parameter. Otherwise the print_status function is called
|
---|
| 34 | # with the failure parameter.
|
---|
| 35 | #
|
---|
| 36 |
|
---|
| 37 | evaluate_retval()
|
---|
| 38 | {
|
---|
[4f3aa1c] | 39 | if [ $? = 0 ]
|
---|
[6370fa6] | 40 | then
|
---|
[4f3aa1c] | 41 | print_status success
|
---|
[6370fa6] | 42 | else
|
---|
| 43 | print_status failure
|
---|
| 44 | fi
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | #
|
---|
| 48 | # The print_status prints [ OK ] or [FAILED] to the screen. OK appears
|
---|
| 49 | # in the colour defined by the SUCCESS variable and FAILED appears in
|
---|
| 50 | # the colour defined by the FAILURE variable. Both are printed starting
|
---|
| 51 | # in the colomn defined by the COL variable.
|
---|
| 52 | #
|
---|
| 53 |
|
---|
| 54 | print_status()
|
---|
| 55 | {
|
---|
| 56 |
|
---|
| 57 | #
|
---|
| 58 | # If no parameters are given to the print_status function, print usage
|
---|
| 59 | # information.
|
---|
| 60 | #
|
---|
| 61 |
|
---|
| 62 | if [ $# = 0 ]
|
---|
| 63 | then
|
---|
| 64 | echo "Usage: print_status {success|failure}"
|
---|
| 65 | return 1
|
---|
| 66 | fi
|
---|
| 67 |
|
---|
| 68 | case "$1" in
|
---|
| 69 | success)
|
---|
| 70 | $SET_COL
|
---|
| 71 | echo -n "[ "
|
---|
| 72 | $SUCCESS
|
---|
| 73 | echo -n "OK"
|
---|
| 74 | $NORMAL
|
---|
| 75 | echo " ]"
|
---|
| 76 | ;;
|
---|
| 77 | failure)
|
---|
| 78 | $SET_COL
|
---|
| 79 | echo -n "["
|
---|
| 80 | $FAILURE
|
---|
| 81 | echo -n "FAILED"
|
---|
| 82 | $NORMAL
|
---|
| 83 | echo "]"
|
---|
| 84 | ;;
|
---|
| 85 | esac
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | #
|
---|
| 90 | # The loadproc function starts a process (often a daemon) with
|
---|
| 91 | # proper error checking
|
---|
| 92 | #
|
---|
| 93 |
|
---|
| 94 | loadproc()
|
---|
| 95 | {
|
---|
| 96 |
|
---|
| 97 | #
|
---|
| 98 | # If no parameters are given to the print_status function, print usage
|
---|
[4f3aa1c] | 99 |
|
---|
[6370fa6] | 100 | # information.
|
---|
| 101 |
|
---|
| 102 | #
|
---|
| 103 |
|
---|
| 104 | if [ $# = 0 ]
|
---|
| 105 | then
|
---|
| 106 | echo "Usage: loadproc {program}"
|
---|
| 107 | exit 1
|
---|
| 108 | fi
|
---|
| 109 | #
|
---|
| 110 | # Find the basename of the first parameter (the daemon's name without
|
---|
| 111 | # the path
|
---|
| 112 | # that was provided so /usr/sbin/syslogd becomes plain 'syslogd' after
|
---|
| 113 | # basename ran)
|
---|
| 114 | #
|
---|
| 115 |
|
---|
| 116 | base=$(/usr/bin/basename $1)
|
---|
| 117 | #
|
---|
| 118 | # the pidlist variable will contains the output of the pidof command.
|
---|
| 119 | # pidof will try to find the PID's that belong to a certain string;
|
---|
| 120 | # $base in this case
|
---|
| 121 | #
|
---|
| 122 |
|
---|
| 123 | pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
|
---|
| 124 |
|
---|
| 125 | pid=""
|
---|
| 126 |
|
---|
| 127 | for apid in $pidlist
|
---|
| 128 | do
|
---|
| 129 | if [ -d /proc/$apid ]
|
---|
| 130 | then
|
---|
| 131 | pid="$pid $apid"
|
---|
| 132 | fi
|
---|
| 133 | done
|
---|
| 134 | #
|
---|
| 135 | # If the $pid variable contains anything (from the previous for loop) it
|
---|
| 136 | # means the daemon is already running
|
---|
| 137 | #
|
---|
| 138 |
|
---|
| 139 | if [ ! -n "$pid" ]
|
---|
| 140 | then
|
---|
| 141 | #
|
---|
| 142 | # Empty $pid variable means it's not running, so we run $* (all
|
---|
| 143 | # parameters giving to this function from the script) and then check the
|
---|
| 144 | # return value
|
---|
| 145 | #
|
---|
| 146 | $*
|
---|
| 147 | evaluate_retval
|
---|
| 148 | else
|
---|
| 149 | #
|
---|
| 150 | # The variable $pid was not empty, meaning it was already running. We
|
---|
| 151 | # print [FAILED] now
|
---|
| 152 | #
|
---|
| 153 | print_status failure
|
---|
| 154 | fi
|
---|
| 155 |
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | #
|
---|
| 159 | # The killproc function kills a process with proper error checking
|
---|
| 160 | #
|
---|
| 161 |
|
---|
| 162 | killproc()
|
---|
| 163 | {
|
---|
| 164 |
|
---|
| 165 | #
|
---|
| 166 | # If no parameters are given to the print_status function, print usage
|
---|
| 167 | # information.
|
---|
| 168 |
|
---|
| 169 | #
|
---|
| 170 |
|
---|
| 171 | if [ $# = 0 ]
|
---|
| 172 | then
|
---|
| 173 | echo "Usage: killproc {program} [signal]"
|
---|
| 174 | exit 1
|
---|
| 175 | fi
|
---|
| 176 |
|
---|
| 177 | #
|
---|
| 178 | # Find the basename of the first parameter (the daemon's name without
|
---|
| 179 | # the path
|
---|
| 180 | # that was provided so /usr/sbin/syslogd becomes plain 'syslogd' after
|
---|
| 181 | # basename ran)
|
---|
| 182 | #
|
---|
| 183 |
|
---|
| 184 | base=$(/usr/bin/basename $1)
|
---|
| 185 |
|
---|
| 186 | #
|
---|
| 187 | # Check if we gave a signal to kill the process with (like -HUP, -TERM,
|
---|
| 188 | # -KILL, etc) to this function (the second parameter). If no second
|
---|
| 189 | # parameter was provided set the nolevel variable. Else set the
|
---|
| 190 | # killlevel variable to the value of $2 (the second parameter)
|
---|
| 191 | #
|
---|
| 192 |
|
---|
| 193 | if [ "$2" != "" ]
|
---|
| 194 | then
|
---|
| 195 | killlevel=-$2
|
---|
| 196 | else
|
---|
| 197 | nolevel=1
|
---|
| 198 | fi
|
---|
| 199 |
|
---|
| 200 | #
|
---|
| 201 | # the pidlist variable will contains the output of the pidof command.
|
---|
| 202 | # pidof will try to find the PID's that belong to a certain string;
|
---|
| 203 | # $base in this case
|
---|
| 204 |
|
---|
| 205 | pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
|
---|
| 206 |
|
---|
| 207 | pid=""
|
---|
| 208 |
|
---|
| 209 | for apid in $pidlist
|
---|
| 210 | do
|
---|
| 211 | if [ -d /proc/$apid ]
|
---|
| 212 | then
|
---|
| 213 | pid="$pid $apid"
|
---|
| 214 | fi
|
---|
| 215 | done
|
---|
| 216 |
|
---|
| 217 | #
|
---|
| 218 | # If $pid contains something from the previous for loop it means one or
|
---|
| 219 |
|
---|
| 220 | # more PID's were found that belongs to the processes to be killed
|
---|
[4f3aa1c] | 221 |
|
---|
[6370fa6] | 222 | #
|
---|
| 223 | if [ -n "$pid" ]
|
---|
| 224 | then
|
---|
| 225 | #
|
---|
| 226 | # If no kill level was specified we'll try -TERM first and then sleep
|
---|
| 227 | # for 2 seconds to allow the kill to be completed
|
---|
| 228 | #
|
---|
| 229 | if [ "$nolevel" = 1 ]
|
---|
| 230 | then
|
---|
| 231 | /bin/kill -TERM $pid
|
---|
| 232 | #
|
---|
[4f3aa1c] | 233 | # If after -TERM the PID still exists we'll wait 2 seconds before
|
---|
| 234 | # trying to kill it with -KILL. If the PID still exist after that, wait
|
---|
| 235 | # two more seconds. If the PIDs still exist by then it's safe to assume
|
---|
| 236 | # that we cannot kill these PIDs.
|
---|
[6370fa6] | 237 | #
|
---|
[4f3aa1c] | 238 |
|
---|
| 239 | if /bin/ps h $pid >/dev/null 2>&1
|
---|
[6370fa6] | 240 | then
|
---|
[4f3aa1c] | 241 | /usr/bin/sleep 2
|
---|
| 242 | if /bin/ps h $pid > /dev/null 2>&1
|
---|
| 243 | then
|
---|
| 244 | /bin/kill -KILL $pid
|
---|
| 245 | if /bin/ps h $pid > /dev/null 2>&1
|
---|
| 246 | then
|
---|
| 247 | /usr/bin/sleep 2
|
---|
| 248 | fi
|
---|
| 249 | fi
|
---|
[6370fa6] | 250 | fi
|
---|
[4f3aa1c] | 251 | /bin/ps h $pid >/dev/null 2>&1
|
---|
[6370fa6] | 252 | if [ $? = 0 ]
|
---|
| 253 | then
|
---|
| 254 | #
|
---|
| 255 | # If after the -KILL it still exists it can't be killed for some reason
|
---|
| 256 | # and we'll print [FAILED]
|
---|
| 257 | #
|
---|
| 258 | print_status failure
|
---|
| 259 | else
|
---|
| 260 | #
|
---|
| 261 | # It was killed, remove possible stale PID file in /var/run and
|
---|
| 262 | # print [ OK ]
|
---|
| 263 | #
|
---|
| 264 | /bin/rm -f /var/run/$base.pid
|
---|
| 265 | print_status success
|
---|
| 266 | fi
|
---|
| 267 | else
|
---|
| 268 | #
|
---|
| 269 | # A kill level was provided. Kill with the provided kill level and wait
|
---|
| 270 | # for 2 seconds to allow the kill to be completed
|
---|
| 271 |
|
---|
| 272 | #
|
---|
| 273 | /bin/kill $killlevel $pid
|
---|
[4f3aa1c] | 274 | if /bin/ps h $pid > /dev/null 2>&1
|
---|
| 275 | then
|
---|
| 276 | /usr/bin/sleep 2
|
---|
| 277 | fi
|
---|
| 278 | /bin/ps h $pid >/dev/null 2>&1
|
---|
[6370fa6] | 279 | if [ $? = 0 ]
|
---|
| 280 | then
|
---|
| 281 | #
|
---|
| 282 | # If ps' return value is 0 it means it ran ok which indicates that the
|
---|
| 283 | # PID still exists. This means the process wasn't killed properly with
|
---|
| 284 | # the signal provided. Print [FAILED]
|
---|
| 285 | #
|
---|
| 286 | print_status failure
|
---|
| 287 | else
|
---|
| 288 | #
|
---|
| 289 | # If the return value was 1 or higher it means the PID didn't exist
|
---|
| 290 | # anymore which means it was killed successfully. Remove possible stale
|
---|
| 291 | # PID file and print [ OK ]
|
---|
| 292 | #
|
---|
| 293 | /bin/rm -f /var/run/$base.pid
|
---|
| 294 | print_status success
|
---|
| 295 | fi
|
---|
| 296 | fi
|
---|
| 297 | else
|
---|
| 298 | #
|
---|
| 299 | # The PID didn't exist so we can't attempt to kill it. Print [FAILED]
|
---|
| 300 | #
|
---|
| 301 | print_status failure
|
---|
| 302 | fi
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | #
|
---|
| 306 | # The reloadproc functions sends a signal to a daemon telling it to
|
---|
| 307 | # reload it's configuration file. This is almost identical to the
|
---|
| 308 | # killproc function with the exception that it won't try to kill it with
|
---|
[4f3aa1c] | 309 |
|
---|
[6370fa6] | 310 | # a -KILL signal (aka -9)
|
---|
| 311 | #
|
---|
| 312 |
|
---|
| 313 | reloadproc()
|
---|
| 314 | {
|
---|
| 315 |
|
---|
| 316 | #
|
---|
| 317 | # If no parameters are given to the print_status function, print usage
|
---|
| 318 | # information.
|
---|
| 319 |
|
---|
| 320 | #
|
---|
| 321 |
|
---|
| 322 | if [ $# = 0 ]
|
---|
| 323 | then
|
---|
| 324 | echo "Usage: reloadproc {program} [signal]"
|
---|
| 325 | exit 1
|
---|
| 326 | fi
|
---|
| 327 |
|
---|
| 328 | #
|
---|
| 329 | # Find the basename of the first parameter (the daemon's name without
|
---|
| 330 | # the path
|
---|
| 331 | # that was provided so /usr/sbin/syslogd becomes plain 'syslogd' after
|
---|
| 332 | # basename ran)
|
---|
| 333 | #
|
---|
| 334 |
|
---|
| 335 | base=$(/usr/bin/basename $1)
|
---|
| 336 |
|
---|
| 337 | #
|
---|
| 338 | # Check if we gave a signal to send to the process (like -HUP)
|
---|
| 339 | # to this function (the second parameter). If no second
|
---|
| 340 | # parameter was provided set the nolevel variable. Else set the
|
---|
| 341 | # killlevel variable to the value of $2 (the second parameter)
|
---|
| 342 | #
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | if [ -n "$2" ]
|
---|
| 346 | then
|
---|
| 347 | killlevel=-$2
|
---|
| 348 | else
|
---|
| 349 | nolevel=1
|
---|
| 350 | fi
|
---|
| 351 |
|
---|
| 352 | #
|
---|
| 353 | # the pidlist variable will contains the output of the pidof command.
|
---|
| 354 | # pidof will try to find the PID's that belong to a certain string;
|
---|
| 355 | # $base in this case
|
---|
| 356 |
|
---|
| 357 | #
|
---|
| 358 |
|
---|
| 359 | pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
|
---|
| 360 |
|
---|
| 361 | pid=""
|
---|
| 362 |
|
---|
| 363 | for apid in $pidlist
|
---|
| 364 | do
|
---|
| 365 | if [ -d /proc/$apid ]
|
---|
| 366 | then
|
---|
| 367 | pid="$pid $apid"
|
---|
| 368 | fi
|
---|
| 369 | done
|
---|
| 370 |
|
---|
| 371 | #
|
---|
| 372 | # If $pid contains something from the previous for loop it means one or
|
---|
| 373 | # more PID's were found that belongs to the processes to be reloaded
|
---|
| 374 | #
|
---|
| 375 |
|
---|
| 376 | if [ -n "$pid" ]
|
---|
| 377 | then
|
---|
| 378 |
|
---|
| 379 | #
|
---|
| 380 | # If nolevel was set we will use the default reload signal SIGHUP.
|
---|
[4f3aa1c] | 381 |
|
---|
[6370fa6] | 382 | #
|
---|
| 383 |
|
---|
| 384 | if [ "$nolevel" = 1 ]
|
---|
| 385 | then
|
---|
| 386 | /bin/kill -SIGHUP $pid
|
---|
| 387 | evaluate_retval
|
---|
| 388 | else
|
---|
| 389 | #
|
---|
| 390 | # Else we will use the provided signal
|
---|
| 391 | #
|
---|
| 392 |
|
---|
| 393 | /bin/kill $killlevel $pid
|
---|
| 394 | evaluate_retval
|
---|
| 395 | fi
|
---|
| 396 | else
|
---|
| 397 | #
|
---|
| 398 | # If $pid is empty no PID's have been found that belong to the process
|
---|
| 399 | # and print [FAILED]
|
---|
| 400 | #
|
---|
| 401 |
|
---|
| 402 | print_status failure
|
---|
| 403 | fi
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | #
|
---|
| 407 | # The statusproc function will try to find out if a process is running
|
---|
| 408 | # or not
|
---|
| 409 | #
|
---|
| 410 |
|
---|
| 411 | statusproc()
|
---|
| 412 | {
|
---|
| 413 |
|
---|
| 414 | #
|
---|
| 415 | # If no parameters are given to the print_status function, print usage
|
---|
| 416 | # information.
|
---|
| 417 |
|
---|
| 418 | #
|
---|
| 419 |
|
---|
| 420 | if [ $# = 0 ]
|
---|
| 421 | then
|
---|
| 422 | echo "Usage: status {program}"
|
---|
| 423 | return 1
|
---|
| 424 | fi
|
---|
| 425 |
|
---|
| 426 | #
|
---|
| 427 | # $pid will contain a list of PID's that belong to a process
|
---|
| 428 | #
|
---|
| 429 |
|
---|
| 430 | pid=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $1)
|
---|
| 431 | if [ -n "$pid" ]
|
---|
| 432 | then
|
---|
| 433 | #
|
---|
| 434 | # If $pid contains something, the process is running, print the contents
|
---|
| 435 | # of the $pid variable
|
---|
| 436 | #
|
---|
| 437 | echo "$1 running with Process ID $pid"
|
---|
| 438 | return 0
|
---|
| 439 | fi
|
---|
| 440 |
|
---|
| 441 | #
|
---|
| 442 | # If $pid doesn't contain it check if a PID file exists and inform the
|
---|
| 443 | # user about this stale file.
|
---|
| 444 | #
|
---|
| 445 |
|
---|
| 446 | if [ -f /var/run/$1.pid ]
|
---|
| 447 | then
|
---|
| 448 | pid=$(/usr/bin/head -1 /var/run/$1.pid)
|
---|
| 449 | if [ -n "$pid" ]
|
---|
| 450 | then
|
---|
| 451 | echo "$1 not running but /var/run/$1.pid exists"
|
---|
| 452 | return 1
|
---|
| 453 | fi
|
---|
| 454 | else
|
---|
| 455 | echo "$1 is not running"
|
---|
| 456 | fi
|
---|
| 457 |
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | # End /etc/init.d/functions
|
---|
| 461 | <userinput>EOF</userinput>
|
---|
| 462 |
|
---|
| 463 | </literallayout>
|
---|
| 464 |
|
---|
| 465 | </sect1>
|
---|
| 466 |
|
---|