这几天学习了一下heartbeat的资料,想参照 Two Apache Web Servers in an Active/Active Configuration    做个Two Nginx Web Services in Active/Active HA。研究发现,heartbeat所管理的资源代理(Resource Agent),有OCF、LSB等几种 。LSB这种shell脚本我还能依葫芦画瓢的写一点,参考linux系统中的原有脚本和网上资料,写了一个很简单的:

C代码 复制代码

  1. #!/bin/bash  
  2. #  
  3. # nginx:        Control the nginx Daemon  
  4. #  
  5. # Version:       @(#) /etc/init.d/nginx 0.1  
  6. #  
  7. # description: This is a init.d script for nginx. Tested on CentOS4.   
  8. #               Change DAEMON and PIDFILE if neccessary.  
  9. #  
  10.   
  11. #Location of nginx binary. Change path as neccessary  
  12. DAEMON=/usr/local/nginx/sbin/nginx  
  13. NAME=`basename $DAEMON`  
  14.   
  15. #Pid file of nginx, should be matched with pid directive in nginx config file.  
  16. PIDFILE=/var/run/$NAME.pid  
  17.   
  18. #this file location  
  19. SCRIPTNAME=/etc/init.d/$NAME  
  20.   
  21. #only run if binary can be found  
  22. test -x $DAEMON || exit 0  
  23.   
  24. RETVAL=0  
  25.   
  26. start() {  
  27.      echo $"Starting $NAME"  
  28.      $DAEMON  
  29.      RETVAL=0  
  30. }  
  31.   
  32. stop() {  
  33.      echo $"Graceful stopping $NAME"  
  34.      [ -s "$PIDFILE" ] && kill -QUIT `cat $PIDFILE`  
  35.      RETVAL=0  
  36. }  
  37.   
  38. forcestop() {  
  39.      echo $"Quick stopping $NAME"  
  40.      [ -s "$PIDFILE" ] && kill -TERM `cat $PIDFILE`  
  41.      RETVAL=$?  
  42. }  
  43.   
  44. reload() {  
  45.      echo $"Graceful reloading $NAME configuration"  
  46.      [ -s "$PIDFILE" ] && kill -HUP `cat $PIDFILE`  
  47.      RETVAL=$?  
  48. }  
  49.   
  50. status() {  
  51.     if [ -s $PIDFILE ]; then  
  52.          echo $"$NAME is running."  
  53.          RETVAL=0  
  54.     else  
  55.          echo $"$NAME stopped."  
  56.          RETVAL=3  
  57.      fi  
  58. }  
  59. # See how we were called.  
  60. case "$1" in  
  61.      start)  
  62.          start  
  63.          ;;  
  64.      stop)  
  65.          stop  
  66.          ;;  
  67.      force-stop)  
  68.          forcestop  
  69.          ;;  
  70.      restart)  
  71.          stop  
  72.          start  
  73.          ;;  
  74.      reload)  
  75.          reload  
  76.          ;;  
  77.      status)  
  78.          status  
  79.          ;;  
  80.      *)  
  81.          echo $"Usage: $0 {start|stop|force-stop|restart|reload|status}"  
  82.          exit 1  
  83. esac  
  84.   
  85. exit $RETVAL  

#!/bin/bash # # nginx: Control the nginx Daemon # # Version: @(#) /etc/init.d/nginx 0.1 # # description: This is a init.d script for nginx. Tested on CentOS4. # Change DAEMON and PIDFILE if neccessary. # #Location of nginx binary. Change path as neccessary DAEMON=/usr/local/nginx/sbin/nginx NAME=`basename $DAEMON` #Pid file of nginx, should be matched with pid directive in nginx config file. PIDFILE=/var/run/$NAME.pid #this file location SCRIPTNAME=/etc/init.d/$NAME #only run if binary can be found test -x $DAEMON || exit 0 RETVAL=0 start() { echo $"Starting $NAME" $DAEMON RETVAL=0 } stop() { echo $"Graceful stopping $NAME" [ -s "$PIDFILE" ] && kill -QUIT `cat $PIDFILE` RETVAL=0 } forcestop() { echo $"Quick stopping $NAME" [ -s "$PIDFILE" ] && kill -TERM `cat $PIDFILE` RETVAL=$? } reload() { echo $"Graceful reloading $NAME configuration" [ -s "$PIDFILE" ] && kill -HUP `cat $PIDFILE` RETVAL=$? } status() { if [ -s $PIDFILE ]; then echo $"$NAME is running." RETVAL=0 else echo $"$NAME stopped." RETVAL=3 fi } # See how we were called. case "$1" in start) start ;; stop) stop ;; force-stop) forcestop ;; restart) stop start ;; reload) reload ;; status) status ;; *) echo $"Usage: $0 {start|stop|force-stop|restart|reload|status}" exit 1 esac exit $RETVAL

注意对应的Nginx配置的PID指令。

参考资料:

LSBResourceAgent

Running and Controlling Nginx

Boot Script for Nginx on Ubuntu, Debian etc.