參考鏈接:
ubuntu-18.04 設置開機啓動腳本
systemd 默認讀取 /etc/systemd/system 下的配置文件,該目錄下的文件會鏈接/lib/systemd/system/下的文件。
執行 ls /lib/systemd/system 你可以看到有很多啓動腳本,其中就有我們需要的 rc.local.service
打開腳本內容:
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
一般正常的啓動文件主要分成三部分
[Unit] 段: 啓動順序與依賴關係
[Service] 段: 啓動行爲,如何啓動,啓動類型
[Install] 段: 定義如何安裝這個配置文件,即怎樣做到開機啓動
可以看出,/etc/rc.local 的啓動順序是在網絡後面,但是顯然它少了 Install 段,也就沒有定義如何做到開機啓動,所以顯然這樣配置是無效的。 因此我們就需要在後面幫他加上 [Install] 段:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
這裏需要注意一下,ubuntu-18.04 默認是沒有 /etc/rc.local 這個文件的,需要自己創建
sudo touch /etc/rc.local
然後把你需要啓動腳本寫入 /etc/rc.local
#!/bin/bash
echo "start a custom-service" > /usr/local/jar/custom-service.log
nohup /usr/local/java/jdk1.8.0_231/bin/java -jar /usr/local/jar/admin.jar &
nohup /usr/local/software/activemq/bin/./activemq start &
exit 0
activemq開機運行中,需要在activemq.sh文件中添加JAVA_HOME(有待進一步確認)
做完這一步,還需要最後一步 前面我們說 systemd 默認讀取 /etc/systemd/system 下的配置文件, 所以還需要在 /etc/systemd/system 目錄下創建軟鏈接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
OK, 接下來,重啓系統reboot
可能遇到的問題:
增加可執行權限:chmod +x /etc/rc.d/rc.local