Advertisement
Guest User

nginx sysv

a guest
Aug 3rd, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides: nginx
  5. # Required-Start: $local_fs $remote_fs $network $syslog $named
  6. # Required-Stop: $local_fs $remote_fs $network $syslog $named
  7. # Default-Start: 2 3 4 5
  8. # Default-Stop: 0 1 6
  9. # Short-Description: starts the nginx web server
  10. # Description: starts nginx using start-stop-daemon
  11. ### END INIT INFO
  12.  
  13. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  14. DAEMON=/usr/sbin/nginx
  15. NAME=nginx
  16. DESC=nginx
  17.  
  18. # Include nginx defaults if available
  19. if [ -r /etc/default/nginx ]; then
  20. . /etc/default/nginx
  21. fi
  22.  
  23. STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"
  24.  
  25. test -x $DAEMON || exit 0
  26.  
  27. . /lib/init/vars.sh
  28. . /lib/lsb/init-functions
  29.  
  30. # Try to extract nginx pidfile
  31. PID=$(cat /etc/nginx/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
  32. if [ -z "$PID" ]; then
  33. PID=/run/nginx.pid
  34. fi
  35.  
  36. if [ -n "$ULIMIT" ]; then
  37. # Set ulimit if it is set in /etc/default/nginx
  38. ulimit $ULIMIT
  39. fi
  40.  
  41. start_nginx() {
  42. # Start the daemon/service
  43. #
  44. # Returns:
  45. # 0 if daemon has been started
  46. # 1 if daemon was already running
  47. # 2 if daemon could not be started
  48. start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
  49. || return 1
  50. start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
  51. $DAEMON_OPTS 2>/dev/null \
  52. || return 2
  53. }
  54.  
  55. test_config() {
  56. # Test the nginx configuration
  57. $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
  58. }
  59.  
  60. stop_nginx() {
  61. # Stops the daemon/service
  62. #
  63. # Return
  64. # 0 if daemon has been stopped
  65. # 1 if daemon was already stopped
  66. # 2 if daemon could not be stopped
  67. # other if a failure occurred
  68. start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
  69. RETVAL="$?"
  70. sleep 1
  71. return "$RETVAL"
  72. }
  73.  
  74. reload_nginx() {
  75. # Function that sends a SIGHUP to the daemon/service
  76. start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
  77. return 0
  78. }
  79.  
  80. rotate_logs() {
  81. # Rotate log files
  82. start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
  83. return 0
  84. }
  85.  
  86. upgrade_nginx() {
  87. # Online upgrade nginx executable
  88. # http://nginx.org/en/docs/control.html
  89. #
  90. # Return
  91. # 0 if nginx has been successfully upgraded
  92. # 1 if nginx is not running
  93. # 2 if the pid files were not created on time
  94. # 3 if the old master could not be killed
  95. if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
  96. # Wait for both old and new master to write their pid file
  97. while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
  98. cnt=`expr $cnt + 1`
  99. if [ $cnt -gt 10 ]; then
  100. return 2
  101. fi
  102. sleep 1
  103. done
  104. # Everything is ready, gracefully stop the old master
  105. if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
  106. return 0
  107. else
  108. return 3
  109. fi
  110. else
  111. return 1
  112. fi
  113. }
  114.  
  115. case "$1" in
  116. start)
  117. log_daemon_msg "Starting $DESC" "$NAME"
  118. start_nginx
  119. case "$?" in
  120. 0|1) log_end_msg 0 ;;
  121. 2) log_end_msg 1 ;;
  122. esac
  123. ;;
  124. stop)
  125. log_daemon_msg "Stopping $DESC" "$NAME"
  126. stop_nginx
  127. case "$?" in
  128. 0|1) log_end_msg 0 ;;
  129. 2) log_end_msg 1 ;;
  130. esac
  131. ;;
  132. restart)
  133. log_daemon_msg "Restarting $DESC" "$NAME"
  134.  
  135. # Check configuration before stopping nginx
  136. if ! test_config; then
  137. log_end_msg 1 # Configuration error
  138. exit $?
  139. fi
  140.  
  141. stop_nginx
  142. case "$?" in
  143. 0|1)
  144. start_nginx
  145. case "$?" in
  146. 0) log_end_msg 0 ;;
  147. 1) log_end_msg 1 ;; # Old process is still running
  148. *) log_end_msg 1 ;; # Failed to start
  149. esac
  150. ;;
  151. *)
  152. # Failed to stop
  153. log_end_msg 1
  154. ;;
  155. esac
  156. ;;
  157. reload|force-reload)
  158. log_daemon_msg "Reloading $DESC configuration" "$NAME"
  159.  
  160. # Check configuration before stopping nginx
  161. #
  162. # This is not entirely correct since the on-disk nginx binary
  163. # may differ from the in-memory one, but that's not common.
  164. # We prefer to check the configuration and return an error
  165. # to the administrator.
  166. if ! test_config; then
  167. log_end_msg 1 # Configuration error
  168. exit $?
  169. fi
  170.  
  171. reload_nginx
  172. log_end_msg $?
  173. ;;
  174. configtest|testconfig)
  175. log_daemon_msg "Testing $DESC configuration"
  176. test_config
  177. log_end_msg $?
  178. ;;
  179. status)
  180. status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
  181. ;;
  182. upgrade)
  183. log_daemon_msg "Upgrading binary" "$NAME"
  184. upgrade_nginx
  185. log_end_msg $?
  186. ;;
  187. rotate)
  188. log_daemon_msg "Re-opening $DESC log files" "$NAME"
  189. rotate_logs
  190. log_end_msg $?
  191. ;;
  192. *)
  193. echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2
  194. exit 3
  195. ;;
  196. esac
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement