美文网首页
Linux Centos7 systemd 配置nginx s

Linux Centos7 systemd 配置nginx s

作者: Bigyong | 来源:发表于2020-04-01 20:49 被阅读0次
    Systemd 是一系列工具的集合,其作用也远远不仅是启动操作系统,它还接管了后台服务、结束、状态查询,以及日志归档、设备管理、电源管理、定时任务等许多职责,并支持通过特定事件(如插入特定 USB 设备)和特定端口数据触发的 On-demand(按需)任务。Systemd 的后台服务还有一个特殊的身份——它是系统中 PID 值为 1 的进程。
    Systemd 目录

    Unit 文件按照 Systemd 约定,应该被放置指定的三个系统目录之一中。这三个目录是有优先级的,如下所示,越靠上的优先级越高。因此,在三个目录中有同名文件的时候,只有优先级最高的目录里的那个文件会被使用。
    /etc/systemd/system:系统或用户自定义的配置文件
    /run/systemd/system:软件运行时生成的配置文件
    /usr/lib/systemd/system:系统或第三方软件安装时添加的配置文件 (常用目录)

    systemd常用的命令
     systemctl restart   服务名       #重起服务
     systemctl start     服务名       #开启服务
     systemctl stop      服务名       #停止服务
     systemctl status    服务名       #查看服务当前的状态
     systemctl enable    服务名       #设置服务开机自启动
     systemctl disable   服务名       #设置服务不开机自启动
    
    配置systemd nginx.service
    cat /usr/lib/systemd/system/nginx.service
    
    [Unit]
    Description=The nginx HTTP and reverse proxy server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    # Nginx will fail to start if /run/nginx.pid already exists but has the wrong
    # SELinux context. This might happen when running `nginx -t` from the cmdline.
    # https://bugzilla.redhat.com/show_bug.cgi?id=1268621
    ExecStartPre=/usr/bin/rm -f /run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    说明
    
    [Unit]
    Description=描述信息
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking#运行方式
    PIDFile=PID进程文件 可以防止启动多副本   后面的kill也需要用到这个PID
    ExecStartPre=开启服务前准备条件
    ExecStart=开启脚本
    ExecReload=重启脚本
    KillSignal=停止信号量
    TimeoutStopSec=停止超时时间
    KillMode=杀掉模式
    PrivateTmp=独立空间
    
    [Install]
    WantedBy=multi-user.target#脚本启动模式,多用户多网络
    
    案例:自动安装 Nginx、Mongodb、Rabbitmq服务监控检查到服务停止自动启动
    cat   monitor.service   #服务配置
    
    [Unit]
    Description=The nginx_monitor server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    #ExecStartPre=/usr/bin/rm -f /run/nginx.pid
    #ExecStartPre=/usr/sbin/nginx -t
    #PIDFile=/run/monitor.pid
    ExecStart=/opt/monitor/monitor.sh       #开启脚本路径使用绝对路径
    ExecReload=/bin/kill -s HUP $MAINPID    #重启脚本是执行 停止服务--开启服务 所以这两个动作需要能执行成功
    ExecStop=/usr/bin/pkill -f monitor.sh   #停止服务的命令或脚本
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target 
    
    cat   monitor.sh   #监控脚本  监控Nginx、Mongodb、Rabbitmq服务检查到服务停止并自动拉起
    
    #!/bin/bash
    ########## nginx
    nginx.monitor(){
    while :
    do
        a=`ps auxc|grep nginx|grep -v grep|wc -l`
        #echo $a
        if [ $a -lt 2 ];then
            /usr/local/nginx/sbin/nginx >/dev/null 2>&1 &
        fi
        sleep 10
    done
    }
    
    ##########  mongod
    mongod.monitor(){
        while :
    do
        a=`ps auxc|grep mongod|grep -v grep|wc -l`
        #echo $a
        if [ $a -lt 1 ];then
            /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf >/dev/null 2>&1 &
        fi
        sleep 10
    done    
    }
    
    ######### rabbitmq
    rabbitmq.monitor(){
        while :
    do
        a=`ps aux|grep rabbitmq|grep -v grep|wc -l`
        #echo $a
        if [ $a -lt 6 ];then
            rabbitmq-server start >/dev/null 2>&1 &
        fi
        sleep 10
    done    
    }
    ######### monitor
    
    nginx.monitor &    #按需要注释或打开监控项
    mongod.monitor &
    rabbitmq.monitor &
    
    cat   install.monitor.sh   #监控安装脚本
    
    #/bin/bash
    mkdir /opt/monitor
    cp ./monitor.sh  /opt/monitor
    cp ./monitor.service  /usr/lib/systemd/system/
    chmod 755 /opt/monitor/monitor.sh
    chmod 644 /usr/lib/systemd/system/monitor.service
    systemctl daemon-reload
    
    cat   delete.monitor.sh   #监控删除脚本
    
    #!/bin/bash
    rm -fr /opt/monitor/monitor.sh  /usr/lib/systemd/system/monitor.service
    systemctl daemon-reload
    
    注意事项:脚本应该是可以后台执行的模式:以下 while: 循环脚本 因为启动有超时限制,systemctl start "服务名" 会被系统认为服务启动超时 所以可以用定义函数方法来解决
    ##########  mongod
    while :
    do
        a=`ps auxc|grep mongod|grep -v grep|wc -l`
        #echo $a
        if [ $a -lt 1 ];then
            /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf >/dev/null 2>&1 &
        fi
        sleep 10
    done   
    

    相关文章

      网友评论

          本文标题:Linux Centos7 systemd 配置nginx s

          本文链接:https://www.haomeiwen.com/subject/lamguhtx.html