functions.xml 12 KB

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