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:

systemctl status

Activates the service "example1" immediately:

systemctl start example1

Deactivates the service "example1" immediately:

# systemctl stop example1

Restarts the service "example1" immediately:

systemctl restart example1

Shows status of the service "example1":

systemctl status example1

Enables "example1" to be started on bootup:

systemctl enable example1

Disables "example1" to not start during bootup:

systemctl disable example1

Update SSID

The following code will create ssid_update.service file.

sudo nano /etc/systemd/system/ssid_update.service

ssid_update.service will execute acc_point_update.sh file

ssid_update.service
[Unit]
Description=Update SSID and PWD
[Service]
ExecStart=/bin/bash acc_point_update.sh
WorkingDirectory=/home/pi/pytobot/systemD
StandardOutput=inherit
StandardError=inherit
User=pi
[Install]
WantedBy=multi-user.target

To start and enable the service the following code is used.

sudo systemctl start ssid_update.service
sudo systemctl enable ssid_update.service

By executing the start command the following code will run:

acc_point_update.sh
#!/bin/bash
SSID=pytobot:$(ifconfig wlan0 | grep -Eo ..\(\:..\){5} | tail -c 9) #update SSID
echo $SSID
PASSWORD=$((10000000 + RANDOM % 9999999)) #update PASSWORD
echo $PASSWORD
sudo sed -i "s/SSIDNAME/$SSID/g" /etc/hostapd/hostapd.conf
sudo sed -i "s/PASSWORD/$PASSWORD/g" /etc/hostapd/hostapd.conf

Screen

The following code will create screen.service file.

sudo nano /etc/systemd/system/screen.service

ssid_update.service will execute screen.service file after network.target is executed

screen.service
[Unit]
Description=print SSID and PASSWORD on OLED
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u screen.py
WorkingDirectory=/home/pi/pytobot/systemD/screen
StandardOutput=inherit
StandardError=inherit
#Restart=always
User=pi
[Install]
WantedBy=multi-user.target

To start and enable the service the following code is used.

sudo systemctl start screen.service
sudo systemctl enable screen.service

Shutdown

The following code will create screen.service file.

sudo nano /etc/systemd/system/shutdown.service

The following script shuts down the Raspberry Pi when the shutdown button is used or the battery voltage is too low.

shutdown.service
[Unit]
Description=Shutdown Raspberry Pi on voltage or switch
[Service]
ExecStart=/usr/bin/python3 -u shutdown.py
WorkingDirectory=/home/pi/pytobot/systemD
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target

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

sudo systemctl start shutdown.service
sudo systemctl enable shutdown.service

By executing the start command the following code will run:

from subprocess import call
import RPi.GPIO as GPIO # import RPi.GPIO module
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN) # set GPIO20 as an input
while True:
if not GPIO.input(26):
print('SYSTEM SHUTDOWN')
call("sudo shutdown -h now", shell=True)

References

SystemD- Raspberry Pi[SOURCE]