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.
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.
It is a flow of setting, but basically it is possible by creating a service file and making the service file usable.
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.
When the service file is complete, load it into the daemon.
$ systemctl daemon-reload
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)
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)
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
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/
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