Supervisor-init-CentOS 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. #
  3. # supervisord This scripts turns supervisord on
  4. #
  5. # Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
  6. # Jason Koppe <jkoppe@indeed.com> adjusted to read sysconfig,
  7. # use supervisord tools to start/stop, conditionally wait
  8. # for child processes to shutdown, and startup later
  9. # Mikhail Mingalev <mingalevme@gmail.com> Merged
  10. # redhat-init-jkoppe and redhat-sysconfig-jkoppe, and
  11. # made the script "simple customizable".
  12. # Brendan Maguire <maguire.brendan@gmail.com> Added OPTIONS to
  13. # SUPERVISORCTL status call
  14. #
  15. # chkconfig: 345 83 04
  16. #
  17. # description: supervisor is a process control utility. It has a web based
  18. # xmlrpc interface as well as a few other nifty features.
  19. # Script was originally written by Jason Koppe <jkoppe@indeed.com>.
  20. #
  21. # source function library
  22. . /etc/rc.d/init.d/functions
  23. set -a
  24. PREFIX=/usr
  25. SUPERVISORD=$PREFIX/bin/supervisord
  26. SUPERVISORCTL=$PREFIX/bin/supervisorctl
  27. PIDFILE=/var/run/supervisord.pid
  28. LOCKFILE=/var/lock/subsys/supervisord
  29. OPTIONS="-c /etc/supervisord.conf"
  30. # unset this variable if you don't care to wait for child processes to shutdown before removing the $LOCKFILE-lock
  31. WAIT_FOR_SUBPROCESSES=yes
  32. # remove this if you manage number of open files in some other fashion
  33. ulimit -n 96000
  34. RETVAL=0
  35. running_pid()
  36. {
  37. # Check if a given process pid's cmdline matches a given name
  38. pid=$1
  39. name=$2
  40. [ -z "$pid" ] && return 1
  41. [ ! -d /proc/$pid ] && return 1
  42. (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
  43. return 0
  44. }
  45. running()
  46. {
  47. # Check if the process is running looking at /proc
  48. # (works for all users)
  49. # No pidfile, probably no daemon present
  50. [ ! -f "$PIDFILE" ] && return 1
  51. # Obtain the pid and check it against the binary name
  52. pid=`cat $PIDFILE`
  53. running_pid $pid $SUPERVISORD || return 1
  54. return 0
  55. }
  56. start() {
  57. echo "Starting supervisord: "
  58. if [ -e $PIDFILE ]; then
  59. echo "ALREADY STARTED"
  60. return 1
  61. fi
  62. # start supervisord with options from sysconfig (stuff like -c)
  63. $SUPERVISORD $OPTIONS
  64. # show initial startup status
  65. $SUPERVISORCTL $OPTIONS status
  66. # only create the subsyslock if we created the PIDFILE
  67. [ -e $PIDFILE ] && touch $LOCKFILE
  68. }
  69. stop() {
  70. echo -n "Stopping supervisord: "
  71. $SUPERVISORCTL $OPTIONS shutdown
  72. if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then
  73. echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"
  74. for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do
  75. if [ ! -e $PIDFILE ] ; then
  76. echo "Supervisord exited as expected in under $total_sleep seconds"
  77. break
  78. else
  79. if [[ $sleep -eq "last" ]] ; then
  80. echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"
  81. return 1
  82. else
  83. sleep $sleep
  84. total_sleep=$(( $total_sleep + $sleep ))
  85. fi
  86. fi
  87. done
  88. fi
  89. # always remove the subsys. We might have waited a while, but just remove it at this point.
  90. rm -f $LOCKFILE
  91. }
  92. restart() {
  93. stop
  94. start
  95. }
  96. case "$1" in
  97. start)
  98. start
  99. RETVAL=$?
  100. ;;
  101. stop)
  102. stop
  103. RETVAL=$?
  104. ;;
  105. restart|force-reload)
  106. restart
  107. RETVAL=$?
  108. ;;
  109. reload)
  110. $SUPERVISORCTL $OPTIONS reload
  111. RETVAL=$?
  112. ;;
  113. condrestart)
  114. [ -f $LOCKFILE ] && restart
  115. RETVAL=$?
  116. ;;
  117. status)
  118. $SUPERVISORCTL $OPTIONS status
  119. if running ; then
  120. RETVAL=0
  121. else
  122. RETVAL=1
  123. fi
  124. ;;
  125. *)
  126. echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
  127. exit 1
  128. esac
  129. exit $RETVAL