Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

樹莓派開機自啟動程式的設定(Arch Linux 版)

要讓樹莓派開機後自動執行某支程式或腳本,得先搞清楚發行版用的是哪套機制。RedHat 系有 /etc/rc.local,把指令寫進去即可;Arch Linux 走的是 systemd 守護進程(daemon)架構。

systemd 由 systemctl 指令管理,它會讀取 .service 檔中關於「啟動什麼」與「何時啟動」的設定,這些檔案放在 /{etc,usr/lib,run}/systemd/system 底下。

常用 systemctl 操作

開機自動啟動/取消:systemctl enable|disable service_name
立即啟動/停止:systemctl start|stop service_name
重啟服務:systemctl restart service_name
查看狀態:systemctl status service_name
檢查是否開機啟動:systemctl is-enabled service_name; echo $?
手動註冊開機服務:ln -sf /lib/systemd/system/ /etc/systemd/system/

完整示範:用 rc.local 觸發自啟動

1. 撰寫 /etc/rc.local

++++++++++++++++++++++++++++++++++++++++++++++++++++

#!/bin/bash

# 定義開機時要執行的指令

echo "exect the application /home/pi/hello" > /dev/ttyAMA0

./home/hello

++++++++++++++++++++++++++++++++++++++++

2. 加上可執行權限

chmod +x /etc/rc.local

3. 建立 service 檔 /usr/lib/systemd/system/rc-local.service

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

4. 建立軟連結

cd /etc/systemd/system/multi-user.target.wants
ln -s /usr/lib/systemd/system/rc-local.service rc-local.service

5. 啟用服務

systemctl enable rc-local.service

6. 驗證

重新開機(reboot),或直接 systemctl start rc-local.service。若開機後程式確實被執行,代表自啟動設定成功。

Copyright © Linux教程網 All Rights Reserved