zoukankan      html  css  js  c++  java
  • 基于CentOS7系统添加自定义脚本服务及参数说明【转】

    概述

    centos6如果要添加自定义脚本服务只需要把脚本放到/etc/init.d然后授权后用chkconfig添加后就可以管理了,那么centos7又是怎么添加自定义脚本服务呢?


    CentOS7添加自定义脚本服务说明

    在CentOS7下,已经不再使用chkconfig命令管理系统开机自启动服务和条件自定义脚本服务了,而是使用管理unit的方式来控制开机自启动服务和添加自定义脚本服务。在/usr/lib/systemd/system目录下包含了各种unit文件,有service后缀的服务unit,有target后缀的开机级别unit等。

    如果想把自定义的脚本变成服务进程,都需要写对应的service配置文件,这样才能被unit所管理(注意:自定义开机自启动服务的.service配置文件必须放在/usr/lib/systemd/system这个目录下面)。服务类别又分为服务又分为系统服务(system)和用户服务(user)。

    • 系统服务:开机不登陆就能运行的程序(常用于开机自启)。
    • 用户服务:需要登陆以后才能运行的程序。

    编写.service配置文件说明

    1、[unit]区块:设置管理启动顺序与依赖关系

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    注意:如果After、Before、Wants、Requires等号后面需要填写多个服务可以用空格隔开。After和Before字段只涉及启动顺序,不涉及依赖关系。Wants字段与Requires字段只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。

    2、[Service]区块:设置启动行为

    •启动命令

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    所有的启动设置之前,都可以加上一个连词号(-),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。例如:ExecStop=-/bin/sh /server/scripts/xx.sh

    •启动类型 Type字段定义启动类型。它可以设置的值如下:

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    •重启行为

    KillMode字段,定义Systemd如何停止服务,它可以设置的值如下

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    Restart字段,定义了服务退出后,Systemd的重启方式,它可以设置的值如下

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    •service区块的其他一些字段

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    3、[Install]区块:定义如何安装这个配置文件,即怎样做到开机启动

    这个设置非常重要,如果设置开机自启动,在/etc/systemd/system目录下面的multi-user.target.wants子目录之中机会创建一个服务的软链接

    WantedBy字段,表示该服务所在的 Targe,target的含义是服务组,表示一组服务,它可以设置的值如下

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    配置文件目录

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例

    实例--配置一个自定义脚本服务

    这里写一个rsync+inotify的脚本服务

    1、创建脚本目录

    mkidr -p /server/scripts/sync.sh
    vim /server/scripts/sync.sh

    2、脚本代码

    #!/bin/bash
    #chkconfig: 2345 38 46
    . /etc/init.d/functions
    if [ $# -ne 1 ]
    then
     echo "usage: $0 {start|stop|status}"
     exit 1
    fi
    case "$1" in
    start)
     if [ -e "/var/run/inotify.pid" ]
     then
     action "inotify service start fail" /bin/false
     echo "sync server is running......"
     sleep 1
     exit 1
     fi
     /bin/bash /server/scripts/inotify.sh &
     `ps -ef|grep "inotifywait"|grep -v "grep"|awk '{print $2}'` >/var/run/inotify.pid
     if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
     then
     action "inotify service is started" /bin/true
     else
     action "inotify service is started" /bin/false
     fi
     ;;
    stop)
     if [ `ps -ef|grep inotify|grep -v grep|wc -l` -a -e "/var/run/inotify.pid" ]
     then
     rm -f /var/run/inotify.pid >/dev/null 2>&1
     pkill inotifywait
     else
     action "inotify service stop fail" /bin/false
     echo "sync server is not running"
     sleep 1
     exit 1
     fi
     sleep 1
     if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 -a ! -e "/var/run/inotify.pid" ]
     then
     action "inotify service is stoped" /bin/true
     else
     action "inotify service is stoped" /bin/false
     fi
     ;;
    status)
     if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
     then
     action "inotify service is running"
     else
     action "inotify service is stoped"
     fi
     ;;
    *)
     echo "usage: $0 {start|stop|status}"
     exit 1
    esac

    3、添加注册脚本服务文件(vim /usr/lib/systemd/system/syncd.service),文件内容如下

    [Unit]
    Description="rsync+inotify实时同步服务"
    After=network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    ExecStart=/bin/sh /server/scripts/sync.sh start
    ExecReload=/bin/sh /server/scripts/sync.sh restart
    ExecStop=/bin/sh /server/scripts/sync.sh stop
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target

    4、运行systemctl start syncd命令启动服务

    转自

    基于CentOS7系统添加自定义脚本服务及参数说明,附实例 https://www.toutiao.com/i6723191120836215304/

  • 相关阅读:
    微信小程序登录
    cURL error 60: SSL certificate problem: unable to get local issuer certificate 报错解决
    MyBatis学习之一----基础了解
    web工程和java工程的区别
    StringUtils的实用功能
    SpringMVC-----部分功能学习
    SpringMVC之国际化
    SpringMVC
    web.xml配置详解
    Hibernate学习-----遇到的相关问题
  • 原文地址:https://www.cnblogs.com/paul8339/p/11376023.html
Copyright © 2011-2022 走看看