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