nginx.init.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh
  2. #
  3. # nginx Startup script for nginx
  4. #
  5. # chkconfig: - 85 15
  6. # processname: %PROVIDES%
  7. # config: /etc/nginx/nginx.conf
  8. # config: /etc/sysconfig/%PROVIDES%
  9. # pidfile: /var/run/nginx.pid
  10. # description: nginx is an HTTP and reverse proxy server
  11. #
  12. ### BEGIN INIT INFO
  13. # Provides: %PROVIDES%
  14. # Required-Start: $local_fs $remote_fs $network
  15. # Required-Stop: $local_fs $remote_fs $network
  16. # Default-Start: %DEFAULTSTART%
  17. # Default-Stop: %DEFAULTSTOP%
  18. # Short-Description: start and stop nginx
  19. ### END INIT INFO
  20. # Source function library.
  21. . /etc/rc.d/init.d/functions
  22. if [ -L $0 ]; then
  23. initscript=`/bin/readlink -f $0`
  24. else
  25. initscript=$0
  26. fi
  27. sysconfig=`/bin/basename $initscript`
  28. if [ -f /etc/sysconfig/$sysconfig ]; then
  29. . /etc/sysconfig/$sysconfig
  30. fi
  31. nginx=${NGINX:-/usr/sbin/%PROVIDES%}
  32. prog=`/bin/basename $nginx`
  33. conffile=${CONFFILE:-/etc/nginx/nginx.conf}
  34. lockfile=${LOCKFILE:-/var/lock/subsys/%PROVIDES%}
  35. pidfile=${PIDFILE:-/var/run/nginx.pid}
  36. SLEEPSEC=${SLEEPSEC:-1}
  37. UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS:-5}
  38. CHECKSLEEP=${CHECKSLEEP:-3}
  39. RETVAL=0
  40. start() {
  41. echo -n $"Starting $prog: "
  42. daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
  43. RETVAL=$?
  44. echo
  45. [ $RETVAL = 0 ] && touch ${lockfile}
  46. return $RETVAL
  47. }
  48. stop() {
  49. echo -n $"Stopping $prog: "
  50. killproc -p ${pidfile} ${prog}
  51. RETVAL=$?
  52. echo
  53. [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
  54. }
  55. reload() {
  56. echo -n $"Reloading $prog: "
  57. killproc -p ${pidfile} ${prog} -HUP
  58. RETVAL=$?
  59. echo
  60. }
  61. upgrade() {
  62. oldbinpidfile=${pidfile}.oldbin
  63. configtest -q || return
  64. echo -n $"Starting new master $prog: "
  65. killproc -p ${pidfile} ${prog} -USR2
  66. echo
  67. for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
  68. /bin/sleep $SLEEPSEC
  69. if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
  70. echo -n $"Graceful shutdown of old $prog: "
  71. killproc -p ${oldbinpidfile} ${prog} -QUIT
  72. RETVAL=$?
  73. echo
  74. return
  75. fi
  76. done
  77. echo $"Upgrade failed!"
  78. RETVAL=1
  79. }
  80. configtest() {
  81. if [ "$#" -ne 0 ] ; then
  82. case "$1" in
  83. -q)
  84. FLAG=$1
  85. ;;
  86. *)
  87. ;;
  88. esac
  89. shift
  90. fi
  91. ${nginx} -t -c ${conffile} $FLAG
  92. RETVAL=$?
  93. return $RETVAL
  94. }
  95. rh_status() {
  96. status -p ${pidfile} -b ${nginx} ${nginx}
  97. }
  98. check_reload() {
  99. templog=`/bin/mktemp --tmpdir nginx-check-reload-XXXXXX.log`
  100. trap '/bin/rm -f $templog' 0
  101. /usr/bin/tail --pid=$$ -n 0 --follow=name /var/log/nginx/error.log > $templog &
  102. /bin/sleep 1
  103. /bin/echo -n $"Sending reload signal to $prog: "
  104. killproc -p ${pidfile} ${prog} -HUP
  105. /bin/echo
  106. /bin/sleep $CHECKSLEEP
  107. /bin/grep -E "\[emerg\]|\[alert\]" $templog
  108. }
  109. # See how we were called.
  110. case "$1" in
  111. start)
  112. rh_status >/dev/null 2>&1 && exit 0
  113. start
  114. ;;
  115. stop)
  116. stop
  117. ;;
  118. status)
  119. rh_status
  120. RETVAL=$?
  121. ;;
  122. restart)
  123. configtest -q || exit $RETVAL
  124. stop
  125. start
  126. ;;
  127. upgrade)
  128. rh_status >/dev/null 2>&1 || exit 0
  129. upgrade
  130. ;;
  131. condrestart|try-restart)
  132. if rh_status >/dev/null 2>&1; then
  133. stop
  134. start
  135. fi
  136. ;;
  137. force-reload|reload)
  138. reload
  139. ;;
  140. configtest)
  141. configtest
  142. ;;
  143. check-reload)
  144. check_reload
  145. RETVAL=0
  146. ;;
  147. *)
  148. echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest|check-reload}"
  149. RETVAL=2
  150. esac
  151. exit $RETVAL