[PYTHON] Programd automatic start at startup with Raspberry Pi 3B + systemd Summary

Start the program at startup on Raspberry Pi

It is inconvenient to open and execute Raspberry Pi commands one by one to execute a Raspberry Pi program. This time, I will briefly summarize how to automatically start a program using systemd at startup.

About systemd

systemd is a mechanism for Linux startup processing and system management. Services are managed as files in units called Units instead of a single shell script. Therefore, the process can be subdivided and executed individually. In addition, since the flow of each process can be clarified, parallel processing is also possible. This time, we will deal with the setting method of program execution at startup, but it can be executed not only at startup but also at the end or with a trigger such as a timer.

Setting flow

It is a flow of setting, but basically it is possible by creating a service file and making the service file usable.

service file creation

Create the service file in the / etc / systemd / system / directory. Therefore, execute the following command. You can use any name you like for the test part of test.service.

$ sudo nano /etc/systemd/system/test.service

Describe as follows in it.

test.service


[Unit]
Description = do test

[Service]
ExecStart=/home/pi/systemd/test.sh
Restart=always
Type=simple
User=pi

[Install]
WantedBy=multi-user.target

[unit] Define the unit itself. Description does not affect the operation, so decide for yourself.

[service] This is where the startup command is determined. If you set with Restart and Type, you can also set restart when the service is stopped. You can also set execute permission in User. By default, in addition to running with sudo privileges, systemd only looks for modules according to the privileges. Therefore, the module pip installed with user authority will be judged as no module, so please do sudo pip install or change the authority setting of the service file to pi.

ExecStart: Service start command Type: How to determine whether the service process has started. The default is "simple". "Simple": Judges that the service has started when the command specified in ExecStart is executed. Restart: Restart condition when service process is stopped (default is "no") "Always": Always try to restart User: Permission to execute (default is "root")

[install] Write this section as a cliché. Without this, the next operation cannot be performed.

Reload service file

When the service file is complete, load it into the daemon.

$ systemctl daemon-reload

Service file operation check

The service file can be started and stopped with the following command. When executing, omit .sevice and enter the service file name you set.

$systemctl start test
$systemctl stop test (stop)

Enable the service file

After confirming the operation, use the following command to decide whether to enable, disable, or disable the service file at startup.

$systemctl enable test (daemon start)
$systemctl disable test (daemon stop)

Check the execution status at startup with a command

If it is executed at startup, it will not flow on the command, so even if an error occurs, it cannot be confirmed. If you execute this command after startup, you can check the log of program execution at startup.

sudo LANG=C systemctl status -l test.service

Referenced

This time, I referred to the following article. https://qiita.com/sinsengumi/items/24d726ec6c761fc75cc9 https://qiita.com/molchiro/items/ee32a11b81fa1dc2fd8d https://tomosoft.jp/design/?p=11697 https://qiita.com/marumen/items/e4c75a2617cb5d0113ce https://www.souichi.club/technology/systemd/

in conclusion

Now you can run it automatically at startup on your Raspberry Pi. If it can be executed automatically, you can start slackbot at startup and operate it from your smartphone. It seems that you can handle the Raspberry Pi more practically.

Recommended Posts

Programd automatic start at startup with Raspberry Pi 3B + systemd Summary
Automatic launch of Raspberry Pi programs using Systemd
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Record temperature and humidity with systemd on Raspberry Pi
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Easy IoT to start with Raspberry Pi and MESH
GPGPU with Raspberry Pi
USB boot with Raspberry Pi 4 Model B (3) LVM edition
DigitalSignage with Raspberry Pi
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Raspberry Pi 4B initial setting
Mutter plants with Raspberry Pi
raspberry pi 1 model b, python
[Raspberry Pi] Stepping motor control with Raspberry Pi
raspberry pi 1 model b, node-red part 17
Servo motor control with Raspberry Pi
Script execution at startup with ipython
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi