daily pastebin goal
71%
SHARE
TWEET

Untitled

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