functions.sgml 11 KB

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