[LINUX] Automatic launch of Raspberry Pi programs using Systemd

Introduction

The content of this article is only the procedure that worked for the time being.

Target application

https://qiita.com/takmot/items/987b493afeeada75925f The UDP receiver program of the above article was created with .NET Core and published for Raspberry Pi.

I have placed this application on / home / pi / work / udp / publish / udp_receiver on the Raspberry Pi. ʻUdp_receiver` is the executable file name.

SystemD Unit file

Create a SystemD Unit file on your PC. The file name should be ʻudp_receiver.service ([executable file name] .service`). The contents are as follows.

[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 is a name and is optional. ʻExecStart` specifies the path of the program to be executed. The rest should be fine.

Auto start setting

Transfer the created SystemD Unit file to Raspberry Pi by FTP. The storage path for the SystemD Unit file will be / etc / systemd / system /. Go to sudo mv udp_receiver.service /etc/systemd/system/ etc.

After that, execute the following command. * ʻUdp_receiver` is the executable file name.

  1. Reload the service
sudo systemctl daemon-reload
  1. Enable automatic service startup
sudo systemctl enable udp_receiver

Below are the execution results. image.png

    1. Start service
sudo systemctl start udp_receiver

Four. Check the operating status of the service

sudo systemctl status udp_receiver

The following is the execution result of 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

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

Five. Restart your Raspberry Pi

sudo reboot
  1. Check if the process is automatically started after restarting
sudo ps -x

It started as follows. image.png

Also, this time we have UDP port 8888 open, so check that as well.

sudo lsof -i:8888

Below are the results. image.png

    1. Stop the program
sudo systemctl stop udp_receiver
    1. Disable autostart
sudo systemctl disable udp_receiver

Problems with auto-launching web apps

For the SystemD Unit file above The execution position of the program is "/".

In the case of a web application, there are js files and css files linked to html, If you specify them as relative paths Since the program goes to see the relative path from "/", there is no file and an error occurs. You can check for errors in this area with your browser's developer tools.

To resolve this issue, modify the SystemD Unit file as follows:

[Unit]
Description=UdpRecv
After=network.target

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

[Install]
WantedBy=multi-user.target

Reference article

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

Recommended Posts

Automatic launch of Raspberry Pi programs using Systemd
Indoor monitoring using Raspberry Pi
Construction of Cortex-M development environment for TOPPERS using Raspberry Pi
USB over ethernet using Raspberry pi
Try using ArUco on Raspberry Pi
Using a webcam with Raspberry Pi
Programd automatic start at startup with Raspberry Pi 3B + systemd Summary
Automatic collection of stock prices using python
Detect "brightness" using python on Raspberry Pi 3!
Make Raspberry Pi speak Japanese using OpenJtalk
Run servomotor on Raspberry Pi 3 using python
Detect temperature using python on Raspberry Pi 3!
Detect slide switches using python on Raspberry Pi 3!
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Send data from Raspberry Pi using AWS IOT
CSV output of pulse data with Raspberry Pi (CSV output)
Get CPU information of Raspberry Pi with Python
Sound the buzzer using python on Raspberry Pi 3!
Create a visitor notification system using Raspberry Pi
Connect your Raspberry Pi to your smartphone using Blynk
Measure CPU temperature of Raspberry Pi with Python
Raspberry Pi backup
Record temperature and humidity with systemd on Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
Create a color sensor using a Raspberry Pi and a camera
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Using Akizuki Denshi's 4WD car FT-MC-004 with Raspberry Pi
Monitor temperature using Raspberry Pi + Alibaba cloud IoT platform
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Implementation of personally optimized photo frames on Raspberry Pi
Control power on / off of USB port of Raspberry Pi
Installation of Docker on Raspberry Pi and L Chika
Output to "7-segment LED" using python on Raspberry Pi 3!
Performance evaluation of investment automatic trading bot using backtesting
Let's operate GPIO of Raspberry Pi with Python CGI
I tried using the Pi Console I / F of the Raspberry Pi IoT starter kit "anyPi" from Mechatrax.
Play to notify Slack of environmental data using AWS PaaS from SensorTag via Raspberry Pi3