Shell脚本配置Centos7开机自启动

/ 前言 /

      在部署过程中, 很多应用都被要求配置开机启动的的服务, 例如MySQL、ElasticSearch等, 每次去写一个启动服务无疑是一键麻烦的事情, 今天我们来看下如何通过Shell脚本来一键生成自启动服务

/ 1 / 脚本

#!/bin/bash
service_file=/etc/systemd/system/init.service

chmod +x $sh_file
touch $service_file

echo '#!/bin/bash' >> $service_file
echo [Unit] >> $service_file
echo Description=init >> $service_file
echo [Service] >> $service_file
echo Type=forking >> $service_file
echo User=root >> $service_file
echo ExecStart=/opt/start.sh >> $service_file
echo [Install] >> $service_file
echo WantedBy=multi-user.target >> $service_file

systemctl enable init
systemctl start init

/ 2 / Service服务参数

  • [Unit]
    • Description : 描述
    • Documentation : 文档
    • Before : 指定在什么服务之前启动, 格式为xxx.service
    • After : 指定在什么服务之后启动, 格式为xxx.service
    • Requires : 启动依赖的单元, 当依赖的单元停止后, 服务也将被关闭
    • Wants : 启动以来的单元, 当依赖的单元停止后, 服务不会被关闭
  • [Service]
    • User : 执行命令或脚本的用户, 例如Elasticsearch需要独立的用户启动
    • ExecStart : 启动服务时执行的命令或者脚本
      • 服务 : 如/opt/elasticsearch/bin/elasticsearch
      • 脚本 : 如/opt/start.sh
    • ExecReload : 重启服务时执行的命令或脚本
    • ExecStop : 停止服务时启动的命令或脚本
    • ExecStartPre : 启动服务之前执行的命令或者脚本
    • ExecStartPost : 启动服务之后执行的命令或者脚本
    • Type : 启动类型
      • simple(默认值) : 启动脚本或命令时的进程为主进程, 如果还要启动其余服务慎用此服务
      • forking : 启动脚本或命令时会以fork方式启动, 父进程会退出
      • notify : 启动后会发出信号通知systemd
      • oneshot : 只执行一次然后退出, 如果你要执行多个任务请不用使用该类型
      • idle : 当所有服务都执行完后才会启动该服务
  • [Install]
    • WantBy : 服务运行所在的服务组, 服务组列表在/etc/systemd/system/下
    • Alias : 别名

/ 3 / 常用命令

  • 开启服务

    systemctl enable init.service
    
    Created symlink from /etc/systemd/system/multi-user.target.wants/init.service to /etc/systemd/system/init.service.
    

    该命令会建立一条链接到服务组

  • 启动服务

    systemctl start init.service
    

    如果报错则说明服务启动失败, 无输出则说明启动成功

  • 停止服务

    systemctl stop init.service
    
  • 查看服务状态

    systemctl status init.service
    
    ● init.service - init
      Loaded: loaded (/etc/systemd/system/init.service; enabled; vendor preset: disabled)
      Active: inactive (dead) since Wed 2020-05-20 05:03:22 EDT; 4s ago
      Process: 7439 ExecStart=/opt/start.sh (code=exited, status=0/SUCCESS)
    
  • 重启服务

    systemctl restart init.service
    
  • 重新加载服务

    systemctl reload init.service
    
  • 报错信息查看

    我们除了可以在systemctl status init.service中看到报错信息外, 如果想看更详细的信息可以执行journalctl -xe命令查看详细信息

     Subject: Unit init.service has begun start-up
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    --
    -- Unit init.service has begun starting up.
    May 20 04:57:31 localhost.localdomain systemd[7351]: Failed at step EXEC spawning /opt/start.sh: No such file or directo
    -- Subject: Process /opt/start.py could not be executed
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    --
    - The process /opt/start.py could not be executed and failed.
    --
    -- The error number returned by this process is 2.
    May 20 04:57:31 localhost.localdomain systemd[1]: init.service: control process exited, code=exited status=203
    May 20 04:57:31 localhost.localdomain systemd[1]: Failed to start zstack a.
    -- Subject: Unit init.service has failed
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    --
    -- Unit i.service has failed.
    --
    -- The result is failed.
    May 20 04:57:31 localhost.localdomain systemd[1]: Unit init.service entered failed state.
    May 20 04:57:31 localhost.localdomain systemd[1]: init.service failed.
    May 20 04:57:31 localhost.localdomain polkitd[6040]: Unregistered Authent
    

    在第5行我们可以看到详细的报错原因, 没有这个文件

  • 报错后重新启动

    • 修改init.service文件
    • 执行systemctl daemon-reload命令
    • 执行systemctl start init.service命令

猜你喜欢

转载自blog.csdn.net/F1004145107/article/details/106238672