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