Supervisor-init-CentOS 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. # Check if a given process pid's cmdline matches a given name
  37. pid=$1
  38. name=$2
  39. [ -z "$pid" ] && return 1
  40. [ ! -d /proc/$pid ] && return 1
  41. (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
  42. return 0
  43. }
  44. running() {
  45. # Check if the process is running looking at /proc
  46. # (works for all users)
  47. # No pidfile, probably no daemon present
  48. [ ! -f "$PIDFILE" ] && return 1
  49. # Obtain the pid and check it against the binary name
  50. pid=`cat $PIDFILE`
  51. running_pid $pid $SUPERVISORD || return 1
  52. return 0
  53. }
  54. start() {
  55. echo "Starting supervisord: "
  56. if [ -e $PIDFILE ]; then
  57. echo "ALREADY STARTED"
  58. return 1
  59. fi
  60. # start supervisord with options from sysconfig (stuff like -c)
  61. $SUPERVISORD $OPTIONS
  62. # show initial startup status
  63. $SUPERVISORCTL $OPTIONS status
  64. # only create the subsyslock if we created the PIDFILE
  65. [ -e $PIDFILE ] && touch $LOCKFILE
  66. }
  67. stop() {
  68. echo -n "Stopping supervisord: "
  69. $SUPERVISORCTL $OPTIONS shutdown
  70. if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then
  71. echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"
  72. for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do
  73. if [ ! -e $PIDFILE ] ; then
  74. echo "Supervisord exited as expected in under $total_sleep seconds"
  75. break
  76. else
  77. if [[ $sleep -eq "last" ]] ; then
  78. echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"
  79. return 1
  80. else
  81. sleep $sleep
  82. total_sleep=$(( $total_sleep + $sleep ))
  83. fi
  84. fi
  85. done
  86. fi
  87. # always remove the subsys. We might have waited a while, but just remove it at this point.
  88. rm -f $LOCKFILE
  89. }
  90. restart() {
  91. stop
  92. start
  93. }
  94. case "$1" in
  95. start)
  96. start
  97. RETVAL=$?
  98. ;;
  99. stop)
  100. stop
  101. RETVAL=$?
  102. ;;
  103. restart|force-reload)
  104. restart
  105. RETVAL=$?
  106. ;;
  107. reload)
  108. $SUPERVISORCTL $OPTIONS reload
  109. RETVAL=$?
  110. ;;
  111. condrestart)
  112. [ -f $LOCKFILE ] && restart
  113. RETVAL=$?
  114. ;;
  115. status)
  116. $SUPERVISORCTL $OPTIONS status
  117. if running ; then
  118. RETVAL=0
  119. else
  120. RETVAL=1
  121. fi
  122. ;;
  123. *)
  124. echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
  125. exit 1
  126. esac
  127. exit $RETVAL