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