2020-10-15|閱讀時間 ‧ 約 3 分鐘

Raspberry pi 開機自動執行 Shell script

前言
大家有這個需求的時候,Google 出來多數都是要去一些地方加一堆東西。但我自己實際測試發現並不用這麼麻煩。因為終究是 Linux,所以回歸到本質上,使用 Linux 的方法設定就好。
本文測試環境: 硬體:Raspberry pi 4 B 8GB OS:Raspberry Pi OS

正文開始
首先先將要執行的內容(例如想要跑 Python 的程式等等)寫成 Shell script 存起來,例如:
#! /bin/bash
echo "hms5232 is Good!" >> /home/pi/example.txt
然後存成 Shell script 並且加上執行的權限
chmod +x /home/pi/example.sh
接著修改/etc/rc.local:
#!/bin/sh -e
#
# 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.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
 printf "My IP address is %s\n" "$_IP"
fi
# 把你要執行的 shell script 放在這邊
/home/pi/example.sh
# 切記,結尾要是 exit 0 這行
exit 0
重新開機後就會看到 example.txt 在/home/pi 底下了。

參考資料

分享至
成為作者繼續創作的動力吧!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言
© 2024 vocus All rights reserved.