Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

CentOS 7 設定開機自動啟動服務與腳本

一、讓既有服務開機自啟

systemd 把這件事簡化成兩個指令,以 Jenkins 為例:

systemctl enable jenkins.service   # 設為開機自啟
systemctl start jenkins.service    # 立刻啟動

用 systemctl is-enabled jenkins 可以回頭確認設定是否生效。

二、讓自訂腳本開機自啟

以下以 autostart.sh 為例,內容就是普通的 shell 腳本:

#!/bin/bash
#description: 開機自啟腳本
/usr/local/tomcat/bin/startup.sh   # 啟動 Tomcat

方法一:掛在 rc.local

先給腳本執行權限:

chmod +x /opt/script/autostart.sh

把路徑寫到 /etc/rc.d/rc.local 最後一行:

/opt/script/autostart.sh

CentOS 7 把 rc.local 的可執行權限拿掉了,因此還要補一道:

chmod +x /etc/rc.d/rc.local

方法二:放進 init.d 並用 chkconfig 註冊

mv /opt/script/autostart.sh /etc/rc.d/init.d
chmod +x /etc/rc.d/init.d/autostart.sh
chkconfig --add autostart.sh
chkconfig autostart.sh on

這種寫法比較正規,也便於日後用 service autostart.sh status 之類的方式管理。兩種做法都在 CentOS 7 上實測可行,挑順手的一種即可。

Copyright © Linux教程網 All Rights Reserved