functions.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <sect1 id="ch07-functions">
  2. <title>Creating the functions script</title>
  3. <para>
  4. A new file <filename>/etc/init.d/functions</filename> is created 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 column 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. #
  170. pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
  171. pid=""
  172. for apid in $pidlist
  173. do
  174. if [ -d /proc/$apid ]
  175. then
  176. pid="$pid $apid"
  177. fi
  178. done
  179. #
  180. # If $pid contains something from the previous for loop it means one or
  181. # more PID's were found that belongs to the processes to be killed
  182. #
  183. if [ -n "$pid" ]
  184. then
  185. #
  186. # If no kill level was specified we'll try -TERM first and then sleep
  187. # for 2 seconds to allow the kill to be completed
  188. #
  189. if [ "$nolevel" = 1 ]
  190. then
  191. /bin/kill -TERM $pid
  192. #
  193. # If after -TERM the PID still exists we'll wait 2 seconds before
  194. # trying to kill it with -KILL. If the PID still exist after that, wait
  195. # two more seconds. If the PIDs still exist by then it's safe to assume
  196. # that we cannot kill these PIDs.
  197. #
  198. if /bin/ps h $pid &gt;/dev/null 2&gt;&amp;1
  199. then
  200. /usr/bin/sleep 2
  201. if /bin/ps h $pid &gt; /dev/null 2&gt;&amp;1
  202. then
  203. /bin/kill -KILL $pid
  204. if /bin/ps h $pid &gt; /dev/null 2&gt;&amp;1
  205. then
  206. /usr/bin/sleep 2
  207. fi
  208. fi
  209. fi
  210. /bin/ps h $pid &gt;/dev/null 2&gt;&amp;1
  211. if [ $? = 0 ]
  212. then
  213. #
  214. # If after the -KILL it still exists it can't be killed for some reason
  215. # and we'll print [FAILED]
  216. #
  217. print_status failure
  218. else
  219. #
  220. # It was killed, remove possible stale PID file in /var/run and
  221. # print [ OK ]
  222. #
  223. /bin/rm -f /var/run/$base.pid
  224. print_status success
  225. fi
  226. else
  227. #
  228. # A kill level was provided. Kill with the provided kill level and wait
  229. # for 2 seconds to allow the kill to be completed
  230. #
  231. /bin/kill $killlevel $pid
  232. if /bin/ps h $pid &gt; /dev/null 2&gt;&amp;1
  233. then
  234. /usr/bin/sleep 2
  235. fi
  236. /bin/ps h $pid &gt;/dev/null 2&gt;&amp;1
  237. if [ $? = 0 ]
  238. then
  239. #
  240. # If ps' return value is 0 it means it ran ok which indicates that the
  241. # PID still exists. This means the process wasn't killed properly with
  242. # the signal provided. Print [FAILED]
  243. #
  244. print_status failure
  245. else
  246. #
  247. # If the return value was 1 or higher it means the PID didn't exist
  248. # anymore which means it was killed successfully. Remove possible stale
  249. # PID file and print [ OK ]
  250. #
  251. /bin/rm -f /var/run/$base.pid
  252. print_status success
  253. fi
  254. fi
  255. else
  256. #
  257. # The PID didn't exist so we can't attempt to kill it. Print [FAILED]
  258. #
  259. print_status failure
  260. fi
  261. }
  262. #
  263. # The reloadproc functions sends a signal to a daemon telling it to
  264. # reload it's configuration file. This is almost identical to the
  265. # killproc function with the exception that it won't try to kill it with
  266. # a -KILL signal (aka -9)
  267. #
  268. reloadproc()
  269. {
  270. #
  271. # If no parameters are given to the print_status function, print usage
  272. # information.
  273. #
  274. if [ $# = 0 ]
  275. then
  276. echo "Usage: reloadproc {program} [signal]"
  277. exit 1
  278. fi
  279. #
  280. # Find the basename of the first parameter (the daemon's name without
  281. # the path that was provided so /usr/sbin/syslogd becomes plain 'syslogd'
  282. # after basename ran)
  283. #
  284. base=$(/usr/bin/basename $1)
  285. #
  286. # Check if we gave a signal to send to the process (like -HUP)
  287. # to this function (the second parameter). If no second
  288. # parameter was provided set the nolevel variable. Else set the
  289. # killlevel variable to the value of $2 (the second parameter)
  290. #
  291. if [ -n "$2" ]
  292. then
  293. killlevel=-$2
  294. else
  295. nolevel=1
  296. fi
  297. #
  298. # the pidlist variable will contains the output of the pidof command.
  299. # pidof will try to find the PID's that belong to a certain string;
  300. # $base in this case
  301. #
  302. pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
  303. pid=""
  304. for apid in $pidlist
  305. do
  306. if [ -d /proc/$apid ]
  307. then
  308. pid="$pid $apid"
  309. fi
  310. done
  311. #
  312. # If $pid contains something from the previous for loop it means one or
  313. # more PID's were found that belongs to the processes to be reloaded
  314. #
  315. if [ -n "$pid" ]
  316. then
  317. #
  318. # If nolevel was set we will use the default reload signal SIGHUP.
  319. #
  320. if [ "$nolevel" = 1 ]
  321. then
  322. /bin/kill -SIGHUP $pid
  323. evaluate_retval
  324. else
  325. #
  326. # Else we will use the provided signal
  327. #
  328. /bin/kill $killlevel $pid
  329. evaluate_retval
  330. fi
  331. else
  332. #
  333. # If $pid is empty no PID's have been found that belong to the process
  334. # and print [FAILED]
  335. #
  336. print_status failure
  337. fi
  338. }
  339. #
  340. # The statusproc function will try to find out if a process is running
  341. # or not
  342. #
  343. statusproc()
  344. {
  345. #
  346. # If no parameters are given to the print_status function, print usage
  347. # information.
  348. #
  349. if [ $# = 0 ]
  350. then
  351. echo "Usage: status {program}"
  352. return 1
  353. fi
  354. #
  355. # $pid will contain a list of PID's that belong to a process
  356. #
  357. pid=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $1)
  358. if [ -n "$pid" ]
  359. then
  360. #
  361. # If $pid contains something, the process is running, print the contents
  362. # of the $pid variable
  363. #
  364. echo "$1 running with Process ID $pid"
  365. return 0
  366. fi
  367. #
  368. # If $pid doesn't contain it check if a PID file exists and inform the
  369. # user about this stale file.
  370. #
  371. if [ -f /var/run/$1.pid ]
  372. then
  373. pid=$(/usr/bin/head -1 /var/run/$1.pid)
  374. if [ -n "$pid" ]
  375. then
  376. echo "$1 not running but /var/run/$1.pid exists"
  377. return 1
  378. fi
  379. else
  380. echo "$1 is not running"
  381. fi
  382. }
  383. # End /etc/init.d/functions
  384. <userinput>EOF</userinput>
  385. </literallayout>
  386. </sect1>