新增指令在Ubuntu 開機時執行

新增指令到 linux 開機啟動時去執行的作法百百種

解法1: 放到 /etc/rcS.d/ 目錄下

這個解法,部份的linux系統無法使用.

先寫一個 .sh 的 shell command, ex: maxserver.sh 放到 /etc/init.d/maxserver.sh

檔案內容如下:

#!/bin/sh
/root/myserver-folder/start.py &>/dev/null &

說明:&>/dev/null & 讓指令丟到背景跑


再讓 /etc/rcS.d/S90myserver.sh 連到 /etc/init.d/myserver.sh

ln -s /etc/init.d/myserver.sh /etc/rcS.d/S90myserver.sh

說明: ln 第1個檔案是隨意找地方放,第2個是放到  /etc/rcS.D/ 目錄裡, S90 是指這個 service 是 啟用中的Serivce 執行的順序是 90 號。

列出所以的serivce

How do you get a list of all starting services?

service --status-all

然後使用指令 service myserver.sh 就可以看到我們的服務了。


解法2: Create a systemd Service

指令:

sudo nano /etc/systemd/system/S15dropboxlike.service

上面的檔案內容:

[Unit]
Description=S15dropboxlike
After=ssh.service network.target
SourcePath=/etc/init.d/dropboxlike.sh
[Service]
User=root
Group=root
Type=forking
Restart=no
TimeoutSec=30
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/init.d/dropboxlike.sh
[Install]
WantedBy=multi-user.target
  • Unit: It stores the metadata and other information you want to store related to the script.
  • Service: Tells the system to execute the desired service, which will run on startup.
  • Install: Allows the service to run the WantedBy directory at the startup to handle the dependencies.

啟用服務的指令:

chmod 644 /etc/systemd/system/S15dropboxlike.service
systemctl enable S15dropboxlike.service

檢查你的服務狀態:

service S15dropboxlike status

如果你有修改 .service changed on disk. Run ‘systemctl daemon-reload‘ to reload units. 當然也可以用 reboot 指令.

下面解法3同解法2.


解法3:

在 CentOS 可以直接修改 /etc/rc.d/rc.local

以前在 Redhat 及 CentOS 要設定開機自動執行的 Shell Script, 一般都會在檔案 /etc/rc.d/rc.local 加入需要執行的 Shell Script 或指令, 但在 CentOS 7 開始, /etc/rc.d/rc.local 預設權限改為 644, 即沒有執行權限, 為甚麼會這樣, 可以開啟 CentOS 7 的 /etc/rc.d/rc.local 看看, 裡面有 Redhat 的說明:

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In constrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

可以看到這個檔案在 RHEL 及 CentOS 7 只為了解決兼容性, Redhat 建議還是自行建立 systemd 服務或者 udev rules 較好, 如果真的需要使用 rc.local, 只要執行 chmod 給予 rc.local 可執行權限即可:

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

執行以上指令後, 下次開機便會自動執行 rc.local 內的指令或 Shell Script.


CentOS ,或是 Ubuntu 18之後的系統,請使用指令

systemctl enable your-service-name

會自動產生一個 your-service-name.service 檔案在路徑:/run/systemd/generator.late/ 目錄下。預設不是 run as root ,會造成重開機之後,不是root 權限,無法使用部份系統資源。

預設的 .service 設定值:

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/myserver.sh

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/init.d/myserver.sh start
ExecStop=/etc/init.d/myserver.sh stop

請修改檔案的內容, 並移動到 /etc/systemd/system/ 下如:

I have created a service file at /etc/systemd/system/ as follows:

[Unit]
Description=MaxServer
After=ssh.service network.target
SourcePath=/etc/init.d/myserver.sh

[Service]
User=root
Group=root
Type=forking
Restart=no
TimeoutSec=30
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/init.d/myserver.sh start

[Install]
WantedBy=multi-user.target

接著再使用 systemctl enable myserver 就可以 enable service.

如果你的 service 需要在 mysql 之後才啟用,請使用這一行:

After=mysql.service

在修改好並移檔案路徑之後,請再下一次指令:

systemctl enable your-service-name

就可以知道,自己的服務有沒有啟用成功,並使用reboot 指令測試是否真的在重啟系統之後,有正確地被自動啟動。


相關文章

Centos 7 Script to install as Linux Service
http://stackoverflow.max-everyday.com/2017/09/centos-7-script-to-install-as-linux-service/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *