编写shell脚本的方式来处理nginx

本文介绍了一种用于控制NGINX服务的Shell脚本,该脚本提供了启动、停止、重启、重载等常用操作,并介绍了如何将其设置为系统服务及开机启动。

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

SHELL编写NGINX服务控制脚本

使用源码包安装的Nginx没办法使用"service nginx start"或"/etc/init.d/nginx start"进行操作和控制,所以写了以下的服务控制脚本。

可以使用的选项有:

start     启动

stop     停止

reload   重载

restart    重启

status    状态

test    检查配置文件

 

1、创建脚本文件并添加执行权限

# touch /etc/init.d/nginx
# chmod +x /etc/init.d/nginx

 

2、编写脚本内容

#!/bin/bash
# chkconfig: - 85 15
# description: Nginx server control script
# processname: nginx
# config file: /usr/local/nginx/conf/nginx.conf
# pid file: /usr/local/nginx/logs/nginx.pid
# 
# eastmoney public tools
# version: v1.0.0
# create by XuHoo, 2016-9-14
# 

# source function library
. /etc/rc.d/init.d/functions

NGINX_NAME="nginx"
NGINX_PROG="/usr/local/sbin/nginx"
NGINX_PID_FILE="/usr/local/nginx/logs/nginx.pid"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
NGINX_LOCK_FILE="/var/lock/subsys/nginx.lock"

# check current user
[ "$USER" != "root" ] && exit 1

start() {
    status
        if [[ $? -eq 0 ]]; then
            echo $"Nginx (PID $(cat $NGINX_PID_FILE)) already started."
            return 1
        fi
    echo -n $"Starting $NGINX_NAME: "
        daemon $NGINX_PROG -c $NGINX_CONF_FILE
        retval=$?
        echo
    [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE
    return $retval
}

stop() {
    status
        if [[ $? -eq 1 ]]; then
            echo "Nginx server already stopped."
            return 1
        fi
    echo -n $"Stoping $NGINX_NAME: "
        killproc $NGINX_PROG
        retval=$?
        echo
    [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE
    return $retval
}

restart() {
    stop
        sleep 1
    start
    retval=$?
    return $retval
}

reload() {
    echo -n $"Reloading $NGINX_NAME: "
        killproc $NGINX_PROG -HUP
        retval=$?
        echo
    return $retval
}

status() {
    netstat -anpt | grep "/nginx" | awk '{print $6}' &> /dev/null
        if [[ $? -eq 0 ]]; then
            if [[ -f $NGINX_LOCK_FILE ]]; then
                return 0
            else
                return 1
            fi
        fi
    return 1
}

_status() {
    status
        if [[ $? -eq 0 ]]; then
            state=`netstat -anpt | grep "/nginx" | awk '{ print $6 }'`
            echo $"Nginx server status is: $state"
        else
            echo "Nginx server is not running"
        fi
}

test() {
    $NGINX_PROG -t -c $NGINX_CONF_FILE
        retval=$?
    return $retval
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        restart
        ;;
    status)
        _status
        ;;
    test)
        test
        ;;
    *)
        echo "Usage: { start | stop | reload | restart | status | test }"
        exit 1
esac

3、将脚本添加到系统服务并设置开机启动

# chkconfig --add nginx
# chkconfig --level 3 nginx on

 

4、测试脚本是否能够执行

运行命令: service nginx start 或 /etc/init.d/nginx start

 

### 编写 Shell 脚本检查 Nginx 的状态和性能 为了有效地监控 Nginx 的运行状况和服务质量,可以创建一个综合性的 Shell 脚本来执行这些任务。下面是一个详细的脚本实现方案。 #### 1. 检查 Nginx 进程是否存在 通过命令 `ps -C nginx --no-header` 可以查询当前是否有名为 nginx 的进程正在运行。如果返回的结果为空,则表示该服务未启动[^1]。 ```bash #!/bin/bash A=$(ps -C nginx --no-header | wc -l) if [ "$A" -eq 0 ]; then echo "Nginx process not found, attempting to restart..." /usr/local/nginx/sbin/nginx fi ``` #### 2. 验证 Nginx 是否监听指定端口 除了确认进程存在外,还需要验证 Nginx 实际上是否绑定了预期的服务端口(通常是80)。可以通过 netstat 或 ss 命令来完成这项工作[^2]。 ```bash PORT="80" B=$(ss -tuln | grep ":$PORT\b") if [[ -z "$B" ]]; then echo "Port $PORT is not listening by any service." else echo "Port $PORT is being listened on." fi ``` #### 3. 使用 systemd 管理器获取更精确的状态信息 对于基于 Systemd 的 Linux 发行版来说,可以直接调用 `systemctl status nginx` 来获得更加详尽的信息关于 Nginx 当前的活动状态。这种方法能够区分出 active 和 inactive 两种不同情形,并据此采取相应措施[^3]。 ```bash STATUS=$(systemctl show -p SubState --value nginx) case $STATUS in "running") echo "Nginx is running normally." ;; *) echo "Nginx seems down or has issues; trying to recover it now..." systemctl start nginx esac ``` #### 4. 日志文件分析 最后一步是对访问日志进行定期审查,以便及时发现问题所在并评估整体性能趋势。这里给出了一种简单的统计方法用于计算特定时间段内的请求次数和其他重要指标[^4]。 ```bash LOG_FILE="/var/log/nginx/access.log" # 统计过去一小时内所有成功的 HTTP 请求 (status code 2xx) SUCCESS_REQUESTS=$(awk '$9 ~ /^2../ {print}' $LOG_FILE \ | awk '{a[$7]++} END{for(i in a){printf "%s\t%d\n",i,a[i];}}') echo "Successful requests within last hour:" echo "$SUCCESS_REQUESTS" ``` 以上就是一套完整的解决方案,它涵盖了从基本健康监测到深入的日志挖掘等多个方面的工作流程。当然,在实际部署之前可能还需要针对具体环境做出适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值