Advertisement
keith_shannon

nginx init.d

Apr 29th, 2015
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.87 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # nginx - this script starts and stops the nginx daemon
  4. #
  5. # chkconfig:   - 85 15
  6. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
  7. #               proxy and IMAP/POP3 proxy server
  8. # processname: nginx
  9. # config:      /etc/nginx/nginx.conf
  10. # config:      /etc/sysconfig/nginx
  11. # pidfile:     /var/run/nginx.pid
  12.  
  13. # Source function library.
  14. . /etc/rc.d/init.d/functions
  15.  
  16. # Source networking configuration.
  17. . /etc/sysconfig/network
  18.  
  19. # Check that networking is up.
  20. [ "$NETWORKING" = "no" ] && exit 0
  21.  
  22. nginx="/usr/sbin/nginx"
  23. prog=$(basename $nginx)
  24.  
  25. sysconfig="/etc/sysconfig/$prog"
  26. lockfile="/var/lock/subsys/nginx"
  27. pidfile="/var/run/${prog}.pid"
  28.  
  29. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  30.  
  31. [ -f $sysconfig ] && . $sysconfig
  32.  
  33.  
  34. start() {
  35.     [ -x $nginx ] || exit 5
  36.     [ -f $NGINX_CONF_FILE ] || exit 6
  37.     echo -n $"Starting $prog: "
  38.     daemon $nginx -c $NGINX_CONF_FILE
  39.     retval=$?
  40.     echo
  41.     [ $retval -eq 0 ] && touch $lockfile
  42.     return $retval
  43. }
  44.  
  45. stop() {
  46.     echo -n $"Stopping $prog: "
  47.     killproc -p $pidfile $prog
  48.     retval=$?
  49.     echo
  50.     [ $retval -eq 0 ] && rm -f $lockfile
  51.     return $retval
  52. }
  53.  
  54. restart() {
  55.     configtest_q || return 6
  56.     stop
  57.     start
  58. }
  59.  
  60. reload() {
  61.     configtest_q || return 6
  62.     echo -n $"Reloading $prog: "
  63.     killproc -p $pidfile $prog -HUP
  64.     echo
  65. }
  66.  
  67. configtest() {
  68.     $nginx -t -c $NGINX_CONF_FILE
  69. }
  70.  
  71. configtest_q() {
  72.     $nginx -t -q -c $NGINX_CONF_FILE
  73. }
  74.  
  75. rh_status() {
  76.     status $prog
  77. }
  78.  
  79. rh_status_q() {
  80.     rh_status >/dev/null 2>&1
  81. }
  82.  
  83. # Upgrade the binary with no downtime.
  84. upgrade() {
  85.     local oldbin_pidfile="${pidfile}.oldbin"
  86.  
  87.     configtest_q || return 6
  88.     echo -n $"Upgrading $prog: "
  89.     killproc -p $pidfile $prog -USR2
  90.     retval=$?
  91.     sleep 1
  92.     if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
  93.         killproc -p $oldbin_pidfile $prog -QUIT
  94.         success $"$prog online upgrade"
  95.         echo
  96.         return 0
  97.     else
  98.         failure $"$prog online upgrade"
  99.         echo
  100.         return 1
  101.     fi
  102. }
  103.  
  104. # Tell nginx to reopen logs
  105. reopen_logs() {
  106.     configtest_q || return 6
  107.     echo -n $"Reopening $prog logs: "
  108.     killproc -p $pidfile $prog -USR1
  109.     retval=$?
  110.     echo
  111.     return $retval
  112. }
  113.  
  114. case "$1" in
  115.     start)
  116.         rh_status_q && exit 0
  117.         $1
  118.         ;;
  119.     stop)
  120.         rh_status_q || exit 0
  121.         $1
  122.         ;;
  123.     restart|configtest|reopen_logs)
  124.         $1
  125.         ;;
  126.     force-reload|upgrade)
  127.         rh_status_q || exit 7
  128.         upgrade
  129.         ;;
  130.     reload)
  131.         rh_status_q || exit 7
  132.         $1
  133.         ;;
  134.     status|status_q)
  135.         rh_$1
  136.         ;;
  137.     condrestart|try-restart)
  138.         rh_status_q || exit 7
  139.         restart
  140.         ;;
  141.     *)
  142.         echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
  143.         exit 2
  144. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement