SystemD
SystemD is a system and service manager for Linux. In order to run a command or program when the Pi boots, it can be added as a service. Once this is done, the command can start/stop enable/disable from the Linux prompt.
Show system status:
1
systemctl status
Copied!
Activates the service "example1" immediately:
1
systemctl start example1
Copied!
Deactivates the service "example1" immediately:
1
# systemctl stop example1
Copied!
Restarts the service "example1" immediately:
1
systemctl restart example1
Copied!
Shows status of the service "example1":
1
systemctl status example1
Copied!
Enables "example1" to be started on bootup:
1
systemctl enable example1
Copied!
Disables "example1" to not start during bootup:
1
systemctl disable example1
Copied!

Update SSID

The following code will create ssid_update.service file.
1
sudo nano /etc/systemd/system/ssid_update.service
Copied!
ssid_update.service will execute acc_point_update.sh file
ssid_update.service
1
[Unit]
2
Description=Update SSID and PWD
3
4
[Service]
5
ExecStart=/bin/bash acc_point_update.sh
6
WorkingDirectory=/home/pi/pytobot/systemD
7
StandardOutput=inherit
8
StandardError=inherit
9
User=pi
10
11
[Install]
12
WantedBy=multi-user.target
Copied!
To start and enable the service the following code is used.
1
sudo systemctl start ssid_update.service
2
sudo systemctl enable ssid_update.service
Copied!
By executing the start command the following code will run:
acc_point_update.sh
1
#!/bin/bash
2
3
SSID=pytobot:$(ifconfig wlan0 | grep -Eo ..\(\:..\){5} | tail -c 9) #update SSID
4
echo $SSID
5
6
PASSWORD=$((10000000 + RANDOM % 9999999)) #update PASSWORD
7
echo $PASSWORD
8
9
sudo sed -i "s/SSIDNAME/$SSID/g" /etc/hostapd/hostapd.conf
10
sudo sed -i "s/PASSWORD/$PASSWORD/g" /etc/hostapd/hostapd.conf
Copied!

Screen

The following code will create screen.service file.
1
sudo nano /etc/systemd/system/screen.service
Copied!
ssid_update.service will execute screen.service file after network.target is executed
screen.service
1
[Unit]
2
Description=print SSID and PASSWORD on OLED
3
After=network.target
4
5
[Service]
6
ExecStart=/usr/bin/python3 -u screen.py
7
WorkingDirectory=/home/pi/pytobot/systemD/screen
8
StandardOutput=inherit
9
StandardError=inherit
10
#Restart=always
11
User=pi
12
13
[Install]
14
WantedBy=multi-user.target
Copied!
To start and enable the service the following code is used.
1
sudo systemctl start screen.service
2
sudo systemctl enable screen.service
Copied!

Shutdown

The following code will create screen.service file.
1
sudo nano /etc/systemd/system/shutdown.service
Copied!
The following script shuts down the Raspberry Pi when the shutdown button is used or the battery voltage is too low.
shutdown.service
1
[Unit]
2
Description=Shutdown Raspberry Pi on voltage or switch
3
4
[Service]
5
ExecStart=/usr/bin/python3 -u shutdown.py
6
WorkingDirectory=/home/pi/pytobot/systemD
7
StandardOutput=inherit
8
StandardError=inherit
9
Restart=always
10
User=pi
11
12
[Install]
13
WantedBy=multi-user.target
Copied!
To start and enable the service the following code is used.
Only enable this service when the Raspberry Pi is inside the PCB. Or Pin 26 needs to have a manual High pin
1
sudo systemctl start shutdown.service
2
sudo systemctl enable shutdown.service
Copied!
By executing the start command the following code will run:
1
from subprocess import call
2
import RPi.GPIO as GPIO # import RPi.GPIO module
3
GPIO.setmode(GPIO.BCM)
4
GPIO.setup(26, GPIO.IN) # set GPIO20 as an input
5
6
while True:
7
if not GPIO.input(26):
8
print('SYSTEM SHUTDOWN')
9
call("sudo shutdown -h now", shell=True)
Copied!

References

SystemD- Raspberry Pi[SOURCE]
Last modified 2yr ago