Raspberry Pi 開機自動執行python程式

 我有自行開發的一支網頁監控程式,需要在樹莓派上啟用python3的http服務。

參考網路上的作法在init.d設定新的daemon沒有成功,手動測試啟動時會出現下列錯誤:

Failed to start prtg.service: Unit not found.

Raspberry Pi的OS版本如下:

pi@raspberrypi:~ $ cat /etc/os-release

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"

經查詢之過才知道OS已將sysvinit改為systemd,依步驟設定好systemd之後終於可以開機自啟動。

1、在程式server_pi.py的第一行加入 #!/usr/bin/python3,並付予執行的權限

# sudo chmod +x server_pi.py

2、在/lib/systemd/system/新增檔案prtg,並將下列內容加入:

[Unit]

Description=Prtg Python Server


[Service]

Type=simple

ExecStart=/home/pi/Desktop/prtg_monitor_fh/Server/app/server_pi.py

Restart=always


[Install]

WantedBy=multi-user.target

3、設定開機時自動啟動

# sudo systemctl enable prtg

4、測試是否可以正確啟動

# sudo systemctl start prtg

5、可以查詢service的狀況

# sudo systemctl status prtg

6、重開樹莓派看看是否開機可以自動啟動


Windows 11安裝時跳過網路連線