Advertisement
Guest User

/etc/init.d/nginx

a guest
Sep 9th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.86 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" ]
  33. then
  34.     PID=/run/nginx.pid
  35. fi
  36.  
  37. # Check if the ULIMIT is set in /etc/default/nginx
  38. if [ -n "$ULIMIT" ]; then
  39.     # Set the ulimits
  40.     ulimit $ULIMIT
  41. fi
  42.  
  43.  
  44. #
  45. # Function that starts the daemon/service
  46. #
  47. do_start()
  48. {
  49.     # Return
  50.     #   0 if daemon has been started
  51.     #   1 if daemon was already running
  52.     #   2 if daemon could not be started
  53.     start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
  54.         || return 1
  55.     start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
  56.         $DAEMON_OPTS 2>/dev/null \
  57.         || return 2
  58. }
  59.  
  60. test_nginx_config() {
  61.     $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
  62. }
  63.  
  64. #
  65. # Function that stops the daemon/service
  66. #
  67. do_stop()
  68. {
  69.     # Return
  70.     #   0 if daemon has been stopped
  71.     #   1 if daemon was already stopped
  72.     #   2 if daemon could not be stopped
  73.     #   other if a failure occurred
  74.     start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
  75.     RETVAL="$?"
  76.  
  77.     sleep 1
  78.     return "$RETVAL"
  79. }
  80.  
  81. #
  82. # Function that sends a SIGHUP to the daemon/service
  83. #
  84. do_reload() {
  85.     start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
  86.     return 0
  87. }
  88.  
  89. #
  90. # Rotate log files
  91. #
  92. do_rotate() {
  93.     start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
  94.     return 0
  95. }
  96.  
  97. #
  98. # Online upgrade nginx executable
  99. #
  100. # "Upgrading Executable on the Fly"
  101. # http://nginx.org/en/docs/control.html
  102. #
  103. do_upgrade() {
  104.     # Return
  105.     #   0 if nginx has been successfully upgraded
  106.     #   1 if nginx is not running
  107.     #   2 if the pid files were not created on time
  108.     #   3 if the old master could not be killed
  109.     if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
  110.         # Wait for both old and new master to write their pid file
  111.         while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
  112.             cnt=`expr $cnt + 1`
  113.             if [ $cnt -gt 10 ]; then
  114.                 return 2
  115.             fi
  116.             sleep 1
  117.         done
  118.         # Everything is ready, gracefully stop the old master
  119.         if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
  120.             return 0
  121.         else
  122.             return 3
  123.         fi
  124.     else
  125.         return 1
  126.     fi
  127. }
  128.  
  129. case "$1" in
  130.     start)
  131.         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  132.         do_start
  133.         case "$?" in
  134.             0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  135.             2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  136.         esac
  137.         ;;
  138.     stop)
  139.         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  140.         do_stop
  141.         case "$?" in
  142.             0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  143.             2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  144.         esac
  145.         ;;
  146.     restart)
  147.         log_daemon_msg "Restarting $DESC" "$NAME"
  148.  
  149.         # Check configuration before stopping nginx
  150.         if ! test_nginx_config; then
  151.             log_end_msg 1 # Configuration error
  152.             exit 0
  153.         fi
  154.  
  155.         do_stop
  156.         case "$?" in
  157.             0|1)
  158.                 do_start
  159.                 case "$?" in
  160.                     0) log_end_msg 0 ;;
  161.                     1) log_end_msg 1 ;; # Old process is still running
  162.                     *) log_end_msg 1 ;; # Failed to start
  163.                 esac
  164.                 ;;
  165.             *)
  166.                 # Failed to stop
  167.                 log_end_msg 1
  168.                 ;;
  169.         esac
  170.         ;;
  171.     reload|force-reload)
  172.         log_daemon_msg "Reloading $DESC configuration" "$NAME"
  173.  
  174.         # Check configuration before reload nginx
  175.         #
  176.         # This is not entirely correct since the on-disk nginx binary
  177.         # may differ from the in-memory one, but that's not common.
  178.         # We prefer to check the configuration and return an error
  179.         # to the administrator.
  180.         if ! test_nginx_config; then
  181.             log_end_msg 1 # Configuration error
  182.             exit 0
  183.         fi
  184.  
  185.         do_reload
  186.         log_end_msg $?
  187.         ;;
  188.     configtest|testconfig)
  189.         log_daemon_msg "Testing $DESC configuration"
  190.         test_nginx_config
  191.         log_end_msg $?
  192.         ;;
  193.     status)
  194.         status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
  195.         ;;
  196.     upgrade)
  197.         log_daemon_msg "Upgrading binary" "$NAME"
  198.         do_upgrade
  199.         log_end_msg 0
  200.         ;;
  201.     rotate)
  202.         log_daemon_msg "Re-opening $DESC log files" "$NAME"
  203.         do_rotate
  204.         log_end_msg $?
  205.         ;;
  206.     *)
  207.         echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2
  208.         exit 3
  209.         ;;
  210. esac
  211.  
  212. :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement