歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 樹莓派開機自啟動程序(Arch Linux 版本)

樹莓派開機自啟動程序(Arch Linux 版本)

日期:2017/3/1 9:57:01   编辑:Linux編程
如何讓樹莓派開機後自動啟動用戶的程序或者執行腳本?

不同的Linux發行版有不同的自啟動機制,如RedHat有 /etc/rc.local 文件,在裡面寫上要執行的命令就可以開機執行。 Arch Linux 采用的是守護進程的機制(daemon)。 在Arch Linux中, 守護進程是用systemd管理的. 用戶用systemctl命令來管理. systemctl讀取.service文件中包含怎麼和什麼時候啟動相關的進程. Service的文件保存在/{etc,usr/lib,run}/systemd/system中. 看看systemd#Using units 有關怎麼使用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/

demo:

1 將腳本寫入/etc/rc.local

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

#!/bin/bash

# this file defines the commands that will be executed at system startup

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

./home/hello

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

2 添加可執行權限

chmod +x /etc/rc.local

3 創建服務文件 /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