functions.xml 12 KB

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