IT|作業系統|Linux|Ubuntu rc.local systemd 開機自動執行應用程式

作業系統版本 Ubuntu 18.04 

使用 systemd 設置開機啓動:
systemd 默認讀取 /etc/systemd/system 下的配置文件該目錄下的文件會鏈接 /lib/systemd/system/ 下的文件。

一般系統安裝完 /lib/systemd/system/ 下會有 rc-local.service文件,即需要的配置文件。
ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service

cd /etc/systemd/system/
cat rc-local.service

rc-local.service内容
# SPDX-License-Identifier: LGPL-2.1+
#
# This fileispart ofsystemd.
#
# systemd isfree software; you can redistribute it and/ormodify it
# under the terms ofthe GNU Lesser General PublicLicense aspublishedby
# the Free Software Foundation; either version 2.1ofthe License, or
# (at your option) any later version.
# This unitgets pulled automatically into multi-user.target by
# systemd-rc-local-generator if/etc/rc.localisexecutable.
[Unit]
Description=/etc/rc.localCompatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.localstart
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

說明
===
1) [Unit] 區塊:啓動順序與依賴關係。
2) [Service] 區塊:啓動行為,如何啓動,啓動類型。
3) [Install] 區塊,定義如何安裝這個配置文件,即怎樣做到開機啓動。

建立 /etc/rc.local 
sudo touch /etc/rc.local
chmod 755 /etc/rc.local


編輯 /etc/rc.local
sudo nano /etc/rc.local

內容如下:
========

#!/bin/bash
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.:


echo "test rc " >> /home/lilee/Signal/project/logs/mylog


/bin/bash /home/lilee/Signal/project/redis-service.sh 1>>   /home/lilee/Signal/project/logs/mylog   # send stderr from rc.local to a log file
2>&1   # send stdout to the same log file

sleep 10

/bin/bash /home/lilee/Signal/project/signal-priority.sh 1>> /home/lilee/Signal/project/logs/mylog   # send stderr from rc.local to a log file
2>&1   # send stdout to the same log file

exit 0

編輯  /home/lilee/Signal/project/redis-service.sh
#!/bin/bash

redis-server /home/lilee/Signal/redis-4.0.1/redis.conf &

exit 0

賦予檔案執行權限
chmod 755 /home/lilee/Signal/project/redis-service.sh

編輯 /home/lilee/Signal/project/signal-priority.sh
#!/bin/bash

cd /home/lilee/Signal/project

java -jar aaron-socket-client-1.0-SNAPSHOT.jar &
java -jar aaron-ui-1.0-SNAPSHOT.jar &

exit 0

賦予檔案執行權限
chmod 755 /home/lilee/Signal/project/redis-service.sh

重啟系統
reboot

這封郵件來自 Evernote。Evernote 是您專屬的工作空間,免費下載 Evernote

留言

這個網誌中的熱門文章

IoT|硬體|樹莓派|外接麥克風及喇叭設置

成長|語文|學習-英文 持續更新!

IoT|硬體|通訊|Arduino 使用 SoftwareSerial Library 與電腦通訊