zoukankan      html  css  js  c++  java
  • CentOS7编写systemd服务脚本

     
     

    简介

    在CentOS6中写服务脚本,需要放在/etc/init.d/目录下,且脚本编写较复杂在。而在CentOS7中写服务脚本,只需要按照格式编写即可。

    CentOS6中服务脚本zabbix_server 参考linux老男孩-shell编程实战185页

    #!/bin/sh
    #chkconfig: 2345 80 05
    #description: zabbix
    path=/user/local/zabbix/sbin
    pid=/tmp/zabbix_server.pid
    conf=/user/local/zabbix/etc/zabbix_server.conf
    RETVAL=0
    . /etc/init.d/functions
    
    start(){
        if [ ! -f $pid ];then
            $path/zabbix_server -c $conf
        RETVAL=$?
        if [ $RETVAL -eq 0 ];then
            action "zabbix_server is started" /bin/true
            return $RETVAL
        else
            action "zabbix_server is started" /bin/false
            return $RETVAL
        fi
        else
        echo "zabbix_server is running"
        return 0
        fi
    
    }
    
    
    stop(){
        if [ -f $pid ];then
            ps -ef |grep zabbix|grep -v grep |awk '{print $2}' |xargs kill -9
            RETVAL=$?
            if [ $RETVAL -eq 0 ];then
                action "zabbix_server is stoped" /bin/true
                return $RETVAL
            else
                action "zabbix_server is stoped" /bin/false
                return $RETVAL
            fi
        else
            echo "zabbix_server is no running"
            return 0
        fi
    
    }
    
    
    case $1 in
        start)
            start
        RETVAL=$?
            ;;
        stop)
        stop
        RETVAL=$?
            ;;
        restart)
        stop
        sleep 1
        start
        RETVAL=$?
            ;;
        status)
            #ps -ef |grep zabbix|grep -v grep |grep -v status
            process=`ps -ef |grep zabbix|grep -v grep |grep -v status |wc -l`
            echo zabbix process $process
            ;;
        *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
            ;;
    esac
    exit $RETVAL
    zabbix_server

    服务脚本编写

    存放位置

    /usr/lib/systemd/system  #系统服务,开机不需要登录就能运行的程序(可以用于开机自启)

    /usr/lib/systemd/user      #用户服务,需要登录后才能运行程序

    服务脚本编写

    服务脚本一般以xxx.service命名,且脚本中分为三部分:[Unit]、[Service]、[Install]

    示例: # vim /usr/lib/systemd/system/xxx.service

    [Unit]   # 主要是服务说明
    Description=test       # 简单描述服务
    After=network.target   # 描述服务类别,表示本服务需要在network服务启动后在启动
    Before=xxx.service    # 表示需要在某些服务启动之前启动,After和Before字段只涉及启动顺序,不涉及依赖关系。
    
    [Service]       # 服务的关键
    Type=forking    # 表示后台运行模式。
    PIDFile=/usr/local/test/test.pid  # 存放PID的绝对路径
    ExecStart=/usr/local/test/bin/startup.sh  # 服务启动命令,命令需要绝对路径
    ExecReload=xxx   #为重启命令,
    ExecStop=xxx     #为停止命令
    PrivateTmp=true  # 表示给服务分配独立的临时空间
    
    [Install] 
    WantedBy=multi-user.target # 多用户
    [Unit]区块字段描述
    
    Description:简短描述
    Documentation:文档地址
    Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败
    Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败
    BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行
    Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动
    After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动
    Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行
    Condition...:当前 Unit 运行必须满足的条件,否则不会运行
    Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败
    
    [Service]区块字段描述
    Type:定义启动时的进程行为。它有以下几种值。
    Type=simple:默认值,执行ExecStart指定的命令,启动主进程
    Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
    Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
    Type=dbus:当前服务通过D-Bus启动
    Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
    Type=idle:若有其他任务执行完毕,当前服务才会运行
    ExecStart:启动当前服务的命令
    ExecStartPre:启动当前服务之前执行的命令
    ExecStartPost:启动当前服务之后执行的命令
    ExecReload:重启当前服务时执行的命令
    ExecStop:停止当前服务时执行的命令
    ExecStopPost:停止当其服务之后执行的命令
    RestartSec:自动重启当前服务间隔的秒数
    Restart:定义何种情况 Systemd 会自动重启当前服务 
        no(默认值): # 退出后无操作
        on-success:  # 只有正常退出时(退出状态码为0),才会重启
        on-failure:  # 非正常退出时,重启,包括被信号终止和超时等
        on-abnormal: # 只有被信号终止或超时,才会重启
        on-abort:    # 只有在收到没有捕捉到的信号终止时,才会重启
        on-watchdog: # 超时退出时,才会重启
        always:      # 不管什么退出原因,都会重启(除了systemctl stop)
        # 对于守护进程,推荐用on-failure
    KillMode的类型:
        control-group(默认):# 当前控制组里的所有子进程,都会被杀掉
        process: # 只杀主进程
        mixed:   # 主进程将收到SIGTERM信号,子进程收到SIGKILL信号
        none:    # 没有进程会被杀掉,只是执行服务的stop命令
    TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
    Environment:指定环境变量
    
    [Install]字段描述
    
    WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
        multi-user.target: # 表示多用户命令行状态,这个设置很重要
        graphical.target:  # 表示图形用户状体,它依赖于multi-user.target
    RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
    Alias:当前 Unit 可用于启动的别名
    Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit
    脚本中分为三部分:[Unit]、[Service]、[Install]详细概述

    nginx服务脚本编写

    [root@n1 ~]# vim /usr/lib/systemd/system/nginx.service

    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecStop=/usr/local/nginx/sbin/nginx -s stop -c /usr/local/nginx/conf/nginx.conf
    ExecReload= /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
    PrivateTmp=ture
    [Install]
    WantedBy=multi-user.target

    命令使用:

    #启动、停止服务
    systemctl start nginx.service
    systemctl stop nginx.service
    #重启服务
    systemctl reload nginx.service #服务启动时重启有效
    systemctl restart nginx.service
    #查看nginx服务状态
    systemctl status nginx.service
    #添加服务到开机自启、取消开机自启
    systemctl enable nginx.service
    systemctl disable nginx.service

    zabbix_server服务脚本编写

     [root@localhost ~]# vim /usr/lib/systemd/system/zabbix_server.service

    [Unit]
    Description=Zabbix Server
    After=syslog.target
    After=network.target
    
    [Service]
    Environment="CONFFILE=/usr/local/zabbix/etc/zabbix_server.conf"
    EnvironmentFile=-/etc/sysconfig/zabbix-server
    Type=forking
    Restart=on-failure
    PIDFile=/tmp/zabbix_server.pid
    KillMode=control-group
    ExecStart=/usr/local/zabbix/sbin/zabbix_server -c $CONFFILE
    ExecStop=/bin/kill -SIGTERM $MAINPID
    RestartSec=10s
    TimeoutSec=0
    
    [Install]
    WantedBy=multi-user.target
    vim /usr/lib/systemd/system/zabbix_agentd.service
    # 添加以服务为名的service文件
    [Unit]
    Desciption=zabbix_agentd - zabbix monitor client
    After=network.target
    # 在network启动后再启动
    #Before=xxx
    # After Before不存在依赖关系,只是启动顺序
     
    [Service]
    User=zabbix
    Group=zabbix
    Type=forking
    # 此服务以forking模式运行
    PIDFile=/tmp/zabbix_agentd.pid
    # PID文件存放位置
    ExecStartPre=/usr/bin/rm -f /tmp/zabbix_agentd.pid
    # 启动前删除PID文件
    ExecStart=/usr/local/zabbix_agent-3.4.15/sbin/zabbix_agentd
    # 启动命令
    ExecReload=/bin/kill -s HUP $MAINPID
    # 重载执行命令
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    # 停止超时时间,如果不能在指定时间内停止,将通过SIGKILL强制终止
    KillMode=mixed
    # systemd停止服务的方式
    Restart=on-failure
    # 服务不正常退出后重启
    #PrivateTmp=true
    # 表示给服务分配独立的临时空间
     
    [Install]
    WantedBy=multi-user.target
    # 多用户模式
    zabbix_agentd.service服务脚本编写

    参考
    ————————————————
    版权声明:本文为CSDN博主「real向往」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/yuanfangpoet/article/details/89410312

  • 相关阅读:
    TextView中文文档
    低版本系统兼容的ActionBar(六)用Fragment+ViewPager+Tab实现快速导航
    低版本系统兼容的ActionBar(五)修改ActionBar的全套样式,从未如此简单过
    低版本系统兼容的ActionBar(四)添加Tab+添加自定义的Tab视图+Fragment
    低版本系统兼容的ActionBar(三)自定义Item视图+进度条的实现+下拉导航+透明ActionBar
    低版本系统兼容的ActionBar(二)ActionProvider+分离式ActionBar+分离式的ActionMode
    Android中获取屏幕长宽的方法
    低版本系统兼容的ActionBar(一)设置颜色+添加Menu+添加ActionMode
    用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)
    Android Studio 下载地址
  • 原文地址:https://www.cnblogs.com/linux985/p/11691774.html
Copyright © 2011-2022 走看看