EnsekiTT Blog

EnsekiTTが書くブログです。

Raspberry Pi 3 Model B+を起動したら所望のネットワークアプリを動かす話

つまりなにしたの?

Raspberry Pi 3 Model B+を起動したときに所望のアプリを
Cronの@rebootを使って起動しようと思ったら、うまく行かなかった。
どうやらネットワークの設定が終わっていないうちに起動してしまうらしく、
スクリプト実行前にsleepを入れたらうまくいくようになった。

f:id:ensekitt:20180802020952j:plain

Raspberry Pi 3 Model B+とは?

教育用ワンボードPCの覇権とも言えるRaspberry Piシリーズの最新版。
amzn.to
www.raspberrypi.org
3 Model B+からPoEに対応したりWiFiBluetoothが最初から搭載されていて、
なんか教育用を大きく逸脱し始めている気がする(褒めてる

Cronの@rebootとは?

ジョブ(スクリプト)を自動実行するためのデーモンプロセス。
よく使うのはバッチ処理などをある時間に実行したりすること、
Reboot時に実行するなども指定できる。
linux.die.net

普通に実行するとどうなるのか?

ネットワークアプリで所望のサイトにアクセスしたり、
ネットワーク上のリソースにアクセスするようなものを実行しようとすると
ネットワークが立ち上がる前に実行されてしまって、うまくいかない。

どうすればよいか?

% crontab -e
# Vi とかが開く

crontab -e で開いたファイル

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

@reboot sleep 30 && /path/to/script

おわりに

ネットワーク関連の処理がうまくいかないときに、
Error処理をして何秒後にリトライとかするべき。
ただサクッとやろうとしたときにはこっちのほうが楽かも。

クリエイティブ・コモンズ・ライセンス
この 作品 は クリエイティブ・コモンズ 表示 4.0 国際 ライセンスの下に提供されています。