functions.xml 12 KB

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