/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.
[root@iZbp11irq8z6b1plo30y63Z ~]# systemctl status nginxnginx.service - LSB: starts the nginx web server Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: active (running) since Fri 2025-03-07 10:28:52 CST; 1h 0min ago Docs: man:systemd-sysv-generator(8) Process: 32241 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS) Tasks: 4 Memory: 50.5M CGroup: /system.slice/nginx.service ├─32253 nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf ├─32254 nginx: worker process ├─32255 nginx: worker process └─32256 nginx: cache manager process Mar 07 10:28:52 iZbp11irq8z6b1plo30y63Z systemd[1]: Starting LSB: starts the nginx web server... Mar 07 10:28:52 iZbp11irq8z6b1plo30y63Z systemd[1]: Started LSB: starts the nginx web server. Mar 07 10:28:52 iZbp11irq8z6b1plo30y63Z nginx[32241]: Starting nginx... done [root@iZbp11irq8z6b1plo30y63Z ~]# ps -ef | grep nginx root 2177 26119 0 10:56 pts/1 00:00:00 sudo vi /etc/yum.repos.d/nginx.repo root 2178 2177 0 10:56 pts/1 00:00:00 vi /etc/yum.repos.d/nginx.repo root 5707 26119 0 11:29 pts/1 00:00:00 grep --color=auto nginx root 32253 1 0 10:28 ? 00:00:00 nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf www 32254 32253 0 10:28 ? 00:00:00 nginx: worker process www 32255 32253 0 10:28 ? 00:00:00 nginx: worker process www 32256 32253 0 10:28 ? 00:00:00 nginx: cache manager process [root@iZbp11irq8z6b1plo30y63Z ~]#
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值