2018年1月9日火曜日

Raspberry Piの起動時にコマンドを自動起動する方法

時代遅れらしい。
起動時にプログラムを自動で実行させます。

rc.localを編集します。

コマンド画面にて
$ sudo nano /etc/rc.local
と入力します。
sudoはroot権限で実行、nanoはエディタです。

--------------------------------------------------------------------------------------------
!/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

exit 0
-------------------------------------------------------------------------------------------

ファイル内容は上記のようになっています。
"exit0" と"fi"の間に実行したいコマンドを記述します。

今回は"/home/pi/hoge.py"というプログラムを実行させたいので下記の一文を追加します。
”$sudo python /home/pi/hoge.py”

下記の状態で保存してます。

--------------------------------------------------------------------------------------------
!/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

$sudo python /home/pi/hoge.py

exit 0
-------------------------------------------------------------------------------------------

以上です。

0 件のコメント:

コメントを投稿