zoukankan      html  css  js  c++  java
  • CentOS7设置自定义开机启动脚本,添加自定义系统服务

    方法一:以systemd管理启动项

                  Centos 系统服务脚本目录:
    /usr/lib/systemd/  
    有系统(system)和用户(user)之分,

    如需要开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即
    /lib/systemd/system/  

    反之,用户登录后才能运行的程序,存在用户(user)里
    服务以.service结尾。

    这边以gitlab开机运行为例

    注/opt/gitlab/ctlscript.sh是gitlab自带的脚本

    [root@em1 system]#vi /usr/lib/systemd/system/gitlab.service
    [Unit]
    Description=gitlab
    After=network.target       

    [Service]
    Type=forking
    ExecStart=/opt/gitlab/ctlscript.sh start
    ExecReload=/opt/gitlab/ctlscript.sh restart
    ExecStop=/opt/gitlab/ctlscript.sh  stop
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target

    将以上脚本以754的权限保存在目录:
    /lib/systemd/system

    说明:
    [Unit]:服务的说明

    Description:描述服务
    After:描述服务类别

    [Service]服务运行参数的设置

    Type=forking是后台运行的形式
    ExecStart为服务的具体运行命令
    ExecReload为重启命令
    ExecStop为停止命令
    PrivateTmp=True表示给服务分配独立的临时空间
    注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

    [Install]服务安装的相关设置,可设置为多用户

    设置开机自启动
    [root@em1 system]# systemctl enable gitlab.service
    ln -s '/usr/lib/systemd/system/gitlab.service' '/etc/systemd/system/multi-user.target.wants/gitlab.service'

    [root@em1 system]#reboot

    其他命令:
    启动nginx服务
    systemctl start gitlab.service

    设置开机自启动
    systemctl enable gitlab.service

    停止开机自启动
    systemctl disable gitlab.service

    查看服务当前状态
    systemctl status gitlab.service

    重新启动服务
    systemctl restart gitlab.service

    查看所有已启动的服务
    systemctl list-units --type=service

    方法二:以init.d管理启动项

    Init.d服务

    cd /etc/rc.d/init.d
    vi nginx

    #!/bin/sh
    #
    #chkconfig: 2345 80 90
    #description: nginx
    
    start() {
        /usr/local/nginx/sbin/nginx
    }
    
    reload() {
        /usr/local/nginx/sbin/nginx -s reload
    }
    
    stop() {
        /usr/local/nginx/sbin/nginx -s quit
    }
    
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      restart)
        stop
        start
        ;;
      reload)
        reload
        ;;
      *)
    echo $"Usage: $0 {start|stop|restart|reload}"
    exit 1
    esac

    chmod +x nginx
    service nginx start
    service nginx stop
    service nginx restart
    service nginx reload
    chkconfig --add nginx
    chkconfig --list

    方法三:以chkconfig管理启动项

    CentOS中使用 chkconfig 命令管理开机启动项

    使用chkconfig 或者 chkconfig –list就可以看出当前系统已经设置的各个服务在各个运行级别下的开闭状态

    chkconfig servicename on/off       //开启或关闭某个服务

    下面以sshd为例:

    chkconfig --list sshd    //查看sshd服务
    chkconfig sshd on      //将 sshd 设置为开机自启动
    chkconfig sshd off      //取消 sshd 的开机自启动
  • 相关阅读:
    CentOS Linux 7 安装教程
    计算机网络概念
    计算机操作系统概念简介
    基础-计算机及操作系统和应用程序的概念
    Spring mvc注解说明
    kaptcha Spring 整合
    Spring常用注解
    Spring bean的自动装配属性
    HQL(Hibernate Query language)语言
    JDBC、Hibernate、Java类型对照表
  • 原文地址:https://www.cnblogs.com/hcs88/p/12673531.html
Copyright © 2011-2022 走看看