1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Systemdを使ったRaspberry Piのプログラムの自動起動

Last updated at Posted at 2020-09-23

はじめに

本記事の内容は、やってみてとりあえず上手くいった手順のみです。

対象アプリケーション

https://qiita.com/takmot/items/987b493afeeada75925f
上記記事のUDP受信プログラムを.NET Coreで作成し、ラズパイ向けにpublishしたものです。

このアプリケーションをラズパイ上の/home/pi/work/udp/publish/udp_receiverに配置しました。udp_receiverは実行ファイル名です。

SystemD Unit ファイル

PCでSystemD Unit ファイルを作成します。
ファイル名はudp_receiver.service[実行ファイル名].service)にします。
内容は以下です。

[Unit]
Description=UdpRecv
After=network.target

[Service]
User=root
Type=simple
ExecStart=/home/pi/work/udp/publish/udp_receiver

[Install]
WantedBy=multi-user.target

Descriptionは名前なので任意です。
ExecStartに実行するプログラムのパスを指定します。
あとはそのままで良いはずです。

自動起動設定

作成したSystemD Unit ファイルをFTPでRaspberry Piに転送。
SystemD Unit ファイルの格納パスは、/etc/systemd/system/になります。
sudo mv udp_receiver.service /etc/systemd/system/等で移動します。

あとは以下コマンドを実行していきます。※udp_receiverは実行ファイル名です。

  1. サービスを再読み込みする
sudo systemctl daemon-reload

2.サービスの自動起動を有効化する

sudo systemctl enable udp_receiver

以下、実行結果です。
image.png

3.サービスを開始する

sudo systemctl start udp_receiver

4.サービスの動作状況を確認する

sudo systemctl status udp_receiver

以下、sudo systemctl status udp_receiver実行結果です。

● udp_receiver.service - UdpRecv
   Loaded: loaded (/etc/systemd/system/udp_receiver.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-09-23 16:33:39 JST; 5s ago
 Main PID: 1240 (udp_receiver)
    Tasks: 8 (limit: 4915)
   Memory: 5.1M
   CGroup: /system.slice/udp_receiver.service
           mq1240 /home/pi/work/udp/publish/udp_receiver

 9月 23 16:33:39 raspberrypi systemd[1]: Started UdpRecv.
 9月 23 16:33:40 raspberrypi udp_receiver[1240]: Receive wait

5.Raspberry Piを再起動する

sudo reboot

6.再起動後プロセスが自動起動しているか確認する

sudo ps -x

以下のように起動していました。
image.png

また、今回はUDPポート8888を開いているのでそれも確認します。

sudo lsof -i:8888

以下、結果です。
image.png

7.プログラムを停止する

sudo systemctl stop udp_receiver

8.自動起動を無効化する

sudo systemctl disable udp_receiver

Webアプリを自動起動した際の問題

上記のSystemD Unit ファイルの場合、
プログラムの実行位置は"/"になります。

Webアプリの場合、htmlに紐づいたjsファイル、cssファイルがあり、
それらを相対パスで指定している場合、
プログラムは"/"からの相対パスを見に行くためファイルがなくエラーになります。
この辺りのエラーは、ブラウザのデベロッパーツールで確認できます。

この問題を解消するためにSystemD Unit ファイルを以下のように変更します。

[Unit]
Description=UdpRecv
After=network.target

[Service]
User=root
Type=simple
WorkingDirectory=/home/pi/work/udp/publish		# 追加
ExecStart=/home/pi/work/udp/publish/udp_receiver

[Install]
WantedBy=multi-user.target

参考記事

https://qiita.com/KEINOS/items/f3e6b3064b0cbe35fd03
https://tomosoft.jp/design/?p=11697

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?