• CentOS8创建开机启动服务


    1、以开机启动EMQX为例。

    2、创建开机启动脚本

    [root@centos8 startup]$ vim emqx-startup.sh
    
    #!/bin/bash
    #description: emqx启动服务
    
    #获取传入命令
    cmd=$1
    
    #获取emqx进程ID
    pid=`ps -ef | grep run_erl | grep /usr/local/emqx/bin/emqx | awk '{print $2}'`
    
    if [ ! $cmd ]; then
      echo "please input cmd {start|restart|stop|status}"
      exit
    fi
    
    #写入日志
    echo "-----------------------------------------------------------------------------"  >> /root/startup/emqx.log
    echo -e "`date +%Y/%D/%H:%M` emqx exec $cmd start." >> /root/startup/emqx.log
    
    if [ $cmd == 'start' ]; then
      if [ ! $pid ]; then
        /usr/local/emqx/bin/emqx start
      else
        echo "emqx is running, pid is $pid."
      fi
    fi
    
    if [ $cmd == 'stop' ]; then
      if [ $pid ]; then
        /usr/local/emqx/bin/emqx stop
      else
        echo "emqx is already stop."
      fi
    fi
    
    if [ $cmd == 'status' ]; then
       /usr/local/emqx/bin/emqx_ctl status
    fi
    
    echo -e "`date +%Y/%D/%H:%M` emqx exec $cmd end." >> /root/startup/emqx.log
    echo " " >> /root/startup/emqx.log

    2、赋予脚本可执行权限

    chmod +x emqx-startup.sh

    3、验证脚本

    [root@centos8 startup]$ ./emqx-startup.sh start
    
    [root@centos8 startup]$ ./emqx-startup.sh stop
    
    [root@centos8 startup]$ ./emqx-startup.sh status

    4、进入进入systemd配置文件目录创建service服务

    [root@centos8 ~]$ cd /usr/lib/systemd/system
    
    [root@centos8 system]$ vim emqx-autorun.service
    
    [Unit]
    Description=emqx for auto start
    Wants=network-online.target
    
    [Service]
    User=root
    Type=forking
    ExecStart=/usr/bin/bash /root/startup/emqx-startup.sh start
    ExecStop=/usr/bin/bash /root/startup/emqx-startup.sh stop
    
    [Install]
    WantedBy=multi-user.target

    5、重新加载systemd配置

    [root@centos8 ~]# systemctl daemon-reload

    6、添加开机自启动

    [root@centos8 system]# systemctl enable emqx-autorun.service
    Created symlink /etc/systemd/system/multi-user.target.wants/emqx-autorun.service → /usr/lib/systemd/system/emqx-autorun.service.

    7、启动停止测试

    [root@centos8 system]# systemctl start emqx-autorun
    [root@centos8 system]# systemctl status emqx-autorun

    8、重启验证

    关于systemd配置说明,传送门:https://www.cnblogs.com/hunttown/p/14872185.html

    欢迎关注我的公众号:云栖语,不一样的研发视界。
    云栖语微信公众号:change-1978
  • 相关阅读:
    hadoop cdh5的pig隐式转化(int到betyarray)不行了
    贝叶斯定理与朴素贝叶斯分类器
    我所见过最全的互联网广告相关介绍
    使用Python做简单的字符串匹配
    awk:快速入门(简单实用19例+鸟哥书内容)
    聚类算法小结
    dubbo 实战
    Mysql查询场景
    深入学习树---二叉树基础
    mysql 索引
  • 原文地址:https://www.cnblogs.com/hunttown/p/14872071.html
Copyright © 2020-2023  润新知