Services, systemctl and logs
A service is a program that runs in the background, usually from boot: Apache (apache2), MariaDB (mariadb), SSH (ssh). systemd starts and watches them; systemctl is how you talk to it; journalctl reads their logs.
The commands
systemctl status apache2 # is it running, and the last log lines (q to quit)
sudo systemctl start apache2 # start now
sudo systemctl stop apache2 # stop now
sudo systemctl restart apache2 # stop + start
sudo systemctl reload apache2 # re-read config without stopping (if the service supports it)
sudo systemctl enable apache2 # start at every boot
sudo systemctl disable apache2 # do not start at boot
sudo systemctl enable --now ssh # enable and start in one go
start/stop affect now. enable/disable affect the next boot. A service can be enabled but stopped, or running but disabled (it will not come back after a reboot).
Quick checks
systemctl is-active apache2 mariadb # active / inactive / failed
systemctl is-enabled apache2 # enabled / disabled
systemctl --failed # every service that failed
Reading systemctl status
● mariadb.service - MariaDB 10.11 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Thu 2026-03-26 14:55:02 +08; 10s ago
Process: 1234 ExecStart=/usr/sbin/mariadbd ... (code=exited, status=1/FAILURE)
Mar 26 14:55:02 raspberrypi mariadbd[1234]: [ERROR] ... No space left on device
| Line | Tells you |
|---|---|
Loaded: ... enabled | Starts at boot |
Active: active (running) | Fine |
Active: inactive (dead) | Stopped (by someone, or never started) |
Active: failed | It crashed or could not start: read the log lines underneath |
activating (auto-restart) | It keeps crashing and systemd keeps retrying |
| Log lines at the bottom | Usually the actual reason |
The dot is green for running, white for stopped, red for failed. Long lines are cut off: use systemctl status apache2 -l --no-pager to see them whole.
Logs with journalctl
sudo journalctl -u mariadb -n 50 --no-pager # last 50 lines of one service
sudo journalctl -u apache2 -b # everything since this boot
sudo journalctl -u apache2 -f # follow live (Ctrl+C to stop)
sudo journalctl -xeu mariadb # jump to the end, with explanations
sudo journalctl -p err -b # only errors, this boot
Apache also writes its own log: /var/log/apache2/error.log. PHP errors go there too.
Old logs take space on the card. Trim them: sudo journalctl --vacuum-size=50M.
Fixing a service that will not start
systemctl status NAMEand read the last lines.sudo journalctl -u NAME -n 50for the full story.- Check the usual causes:
| Log says | Cause | Fix |
|---|---|---|
Syntax error on line ... / AH00526 | Config typo (Apache) | sudo apache2ctl configtest, fix that line |
unknown variable / Found option without preceding group | Config typo (MariaDB) | Check the file named in the log, under /etc/mysql/ |
Address already in use / could not bind | Another program has the port | sudo ss -tlnp to find it |
No space left on device | Card full | Free space |
Read-only file system | Card damaged | Repair |
Permission denied on a file | Wrong owner after editing as root, or a copy without -a | Compare owners with ls -l; MariaDB's files must belong to mysql |
Unit apache2.service not found | Not installed (or a typo) | sudo apt install apache2 |
- Fix, then
sudo systemctl restart NAMEand check the status again.
If it failed many times quickly, systemd stops retrying (start request repeated too quickly). After fixing: sudo systemctl reset-failed NAME, then start it.
Services used in this class
| Service | What | Port |
|---|---|---|
apache2 | Web server (PHP, phpMyAdmin) | 80 |
mariadb | Database | 3306 |
ssh | Remote terminal | 22 |
NetworkManager | Wi-Fi and network |
Flask is not a service: it only runs while you run app.py. (mysql also works as a name for mariadb.)
Your own program as a service
To make a script (for example the Lab 5d logger or a Flask app) start at boot and restart if it crashes, create /etc/systemd/system/sensorlog.service:
[Unit]
Description=Sensor logger
After=network.target mariadb.service
[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/env/bin/python /home/pi/Lab5diii.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload # after creating or editing a .service file
sudo systemctl enable --now sensorlog
systemctl status sensorlog
sudo journalctl -u sensorlog -f # its print() output appears here
Note ExecStart uses the venv's Python, so the venv's packages are found. Stop it before running the same script in Thonny, or both will fight over the serial port.