-
Notifications
You must be signed in to change notification settings - Fork 9
systemd
systemd is an init manager for Linux. It is an alternative to sysvinit or other init manager. As such, this is the first user mode process (PID 1) started from the Linux kernel during boot.
A clean bitbake build is necessary, you might want to use a separate build folder like build_systemd
.
Before starting to bitbake an image, change local.conf accordingly. Add two lines
VIRTUAL-RUNTIME_init_manager = "systemd"
DISTRO_FEATURES_append = " systemd"
and, if necessary, remove or comment out lines specifying different init mechanisms and device file managers like
# PREFERRED_PROVIDER_udev = "eudev"
# VIRTUAL_RUNTIME_init_manager = "sysvinit"
Linux (console-image) boot time from first dmesg entry "Booting Linux..." until serial console login prompt approx. 5 seconds.
By default, the networking service is masked, i.e. it will not start. So, for the first login one cannot use network interfaces. http://0pointer.de/blog/projects/systemd.html To change this behaviour: login, unmask and enable the networking service:
root@raspberrypi3:~# systemctl status networking
root@raspberrypi3:~# systemctl unmask networking
root@raspberrypi3:~# systemctl enable networking
root@raspberrypi3:~# systemctl start networking
start will start the service immediately, enable will enable it during boot. Check network connectivity:
root@raspberrypi3:~# ifconfig
root@raspberrypi3:~# route
root@raspberrypi3:~# ping 8.8.8.8
Lets say we want to blink a LED automatically after boot. We are simply using a shell script for doing so. For testing, a LED should be attached to expansion header pin GPIO18. For the hardware+software basics see GPIO.
Create and edit a new file led_blinking.sh
with the following content:
#! /bin/sh
# signal handler containing termination code for some common signals
trap "echo 18 > /sys/class/gpio/unexport; exit" 1 2 9 15
# initialization code
echo 18 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio18/direction
# main loop
while :
do
echo 1 > /sys/class/gpio/gpio18/value
sleep 1
echo 0 > /sys/class/gpio/gpio18/value
sleep 1
done
It is possible to run led_blinking.sh
from a shell provided that it is executable (chmod +x led_blinking.sh
):
root@raspberrypi3:~# ./led_blinking.sh
Create and edit a new file /etc/systemd/system/led_blinking.service
with the following content:
[Unit]
Description=led blinking service
[Service]
Type=simple
User=root
ExecStart=/home/root/led_blinking.sh
Restart=always
RestartSec=5
StartLimitBurst=5
StartLimitInterval=1m
StartLimitAction=reboot
[Install]
WantedBy=multi-user.target
ExecStart
must contain the absolute path to the script. Note that the path may be different for a different user!
The Restart*
and StartLimit*
lines are optional. If specified, systemd tries to restart the service several times after it failed (crashed) and eventually executes the StartLimitAction (reboots the machine) to recover from such failures.
After creating the service description file, reload systemd:
root@raspberrypi:~ $ sudo systemctl daemon-reload
Now, you may start and stop the service manually and get the service status:
root@raspberrypi:~ $ systemctl start led_blinking.service
root@raspberrypi:~ $ systemctl status led_blinking.service
● led_blinking.service - led blinking service
Loaded: loaded (/etc/systemd/system/led_blinking.service; static)
Active: active (running) since Mon 2017-03-20 08:27:48 UTC; 2s ago
Main PID: 2672 (led_blinking.sh)
CGroup: /system.slice/led_blinking.service
├─2672 /bin/sh /home/pi/led_blinking.sh
└─2685 sleep 1
root@raspberrypi:~ $ systemctl stop led_blinking.service
to start the service automatically after boot, use:
root@raspberrypi:~ $ systemctl enable led_blinking.service
and to no longer start the service automatically after boot, use:
root@raspberrypi:~ $ systemctl disable led_blinking.service
systemd activities are logged. The logs can be queried using journalctl.
sudo journalctl -u led_blinking.service
Enable the service such that it is run automatically after boot. As soon as the LED goes on, kill the service. (Make sure that there are no other imported processes matching the /bin/sh path).
sudo killall /bin/sh
Systemd will restart the service automatically.
If you continue killing new instances quickly, systemd will eventually reboot. Reboot does not help against evildoers, but would help if the services failed because some hardware hung.