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