Kamuycikap - SentenceDataBase

日々の勉強の記録を気分で書き綴るブログ

rc.localからUSBメモリのマウントを確認する

RaspberryPiで起動時にUSBメモリのマウントを確認(rc.local)

参考URL → https://dekuo-03.hatenablog.jp/entry/2019/12/08/000955

logを残す仕組みを作成

自動起動したいcommandの標準出力、エラーをファイルに吐くシェルスクリプトを作成

## commandが実行したいバイナリファイル or シェルスクリプト。 Logを残したいファイル名は適当
command > /path/to/logfile 2>&1

rc.localの編集

USBメモリがマウントされたかどうかは、USBメモリ内部にあるファイル / フォルダが存在するか否かを判断するコードを書く。

pi@raspberrypi:~ $ sudo nano /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

# 最大三十秒。
# /media/pi/A81C-D3AB/fsthermo/param.txt が存在するか否かを確認し続ける。
# 存在したら、fsrun.shを実行した後、breakで抜ける。
for i in `seq 0 30`
do
  if [ -e /media/pi/A81C-D3AB/fsthermo/param.txt ]; then
    /home/pi/Public/fsthermo/fspi/fsrun.sh &    # ← 実行したいコマンド
    break
  fi
  sleep 1s
done

exit 0

rc.localが正しく動作しているか否か確認する

pi@raspberrypi:~ $ sudo systemctl status rc-local.service