/etc/init.d/nginx (lsb style)

本文介绍如何使用 Heartbeat 实现 Nginx 的高可用性集群配置,并提供了一个简单的 LSB 资源代理脚本示例,用于控制 Nginx 服务的启动、停止及状态检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天学习了一下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系统中的原有脚本和网上资料,写了一个很简单的:

 

#!/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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值