nginx.initd 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2017 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. extra_commands="configtest"
  5. extra_started_commands="upgrade reload"
  6. description="Robust, small and high performance http and reverse proxy server"
  7. description_configtest="Run nginx' internal config check."
  8. description_upgrade="Upgrade the nginx binary without losing connections."
  9. description_reload="Reload the nginx configuration without losing connections."
  10. NGINX_CONFIGFILE=${NGINX_CONFIGFILE:-/etc/nginx/nginx.conf}
  11. command="/usr/bin/nginx"
  12. command_args="-c \"${NGINX_CONFIGFILE}\""
  13. start_stop_daemon_args=${NGINX_SSDARGS:-"--wait 1000"}
  14. pidfile=${NGINX_PIDFILE:-/run/nginx.pid}
  15. user=${NGINX_USER:-nginx}
  16. group=${NGINX_GROUP:-nginx}
  17. retry=${NGINX_TERMTIMEOUT:-"TERM/60/KILL/5"}
  18. depend() {
  19. need net
  20. use dns logger netmount
  21. }
  22. start_pre() {
  23. if [ "${RC_CMD}" != "restart" ]; then
  24. configtest || return 1
  25. fi
  26. }
  27. stop_pre() {
  28. if [ "${RC_CMD}" = "restart" ]; then
  29. configtest || return 1
  30. fi
  31. }
  32. stop_post() {
  33. rm -f ${pidfile}
  34. }
  35. reload() {
  36. configtest || return 1
  37. ebegin "Refreshing nginx' configuration"
  38. start-stop-daemon --signal SIGHUP --pidfile "${pidfile}"
  39. eend $? "Failed to reload nginx"
  40. }
  41. upgrade() {
  42. configtest || return 1
  43. ebegin "Upgrading nginx"
  44. einfo "Sending USR2 to old binary"
  45. start-stop-daemon --signal SIGUSR2 --pidfile "${pidfile}"
  46. einfo "Sleeping 3 seconds before pid-files checking"
  47. sleep 3
  48. if [ ! -f "${pidfile}.oldbin" ]; then
  49. eerror "File with old pid not found"
  50. return 1
  51. fi
  52. if [ ! -f "${pidfile}" ]; then
  53. eerror "New binary failed to start"
  54. return 1
  55. fi
  56. einfo "Sleeping 3 seconds before WINCH"
  57. sleep 3
  58. # Cannot send "WINCH" using start-stop-daemon yet, https://bugs.gentoo.org/604986
  59. kill -WINCH $(cat "${pidfile}.oldbin")
  60. einfo "Sending QUIT to old binary"
  61. start-stop-daemon --signal SIGQUIT --pidfile "${pidfile}.oldbin"
  62. einfo "Upgrade completed"
  63. eend $? "Upgrade failed"
  64. }
  65. configtest() {
  66. ebegin "Checking nginx' configuration"
  67. ${command} -c "${NGINX_CONFIGFILE}" -t -q
  68. if [ $? -ne 0 ]; then
  69. ${command} -c "${NGINX_CONFIGFILE}" -t
  70. fi
  71. eend $? "failed, please correct errors above"
  72. }