Centos8设置开机启动服务


方法一:配置rc.local(不推荐)

直接修订文件/etc/rc.local,添加操作命令即可

方法二:chkconfig

新建脚本,开头加入以下代码

#!/bin/sh
#chkconfig:2345 80 90
#description:auto_run

将文件移到/etc/init.d目录下,chmod +x script.sh设置脚本权限

使用chkconfig命令将脚本添加到开机启动

chkconfig --add script.sh
chkconfig script.sh on

方法三:systemctl

  1. 新建脚本,例如,/tmp/mytest.sh,设置权限chmod +x /tmp/mytest.sh

  2. 进入目录/usr/lib/systemd/system,新建服务,例如mytest.service,编辑为如下形式:

    [Unit]
    Description=mytest for auto start
    Wants=network-online.target
    
    [Service]
    User=root
    Type=forking
    ExecStart=/usr/bin/bash /tmp/mytest.sh start
    ExecStop=/usr/bin/bash /tmp/mytest.sh stop
    
    [Install]
    WantedBy=multi-user.target
    
  3. 重新加载systemd配置systemctl daemon-reload

  4. 添加开机自启动systemctl enable mytest.service

  5. reboot重启验证

方法四:profile

直接将写好的脚本(.sh文件)放到目录/etc/profile.d/下。

参考资料:

https://blog.csdn.net/qq_36823799/article/details/117930664

https://www.cnblogs.com/hunttown/p/14872071.html

https://www.cnblogs.com/jingzaixin/p/12752373.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM