But how the systemd knows that single app like mpv crashed? I thought this systemd unit file is set up according to whole system restarts and etc. What about size of the journal? It has any restrictions? I don’t have much free space about 10gb.
Part 1: How Systemd Knows mpv Crashed
When systemd launches a service, it doesn’t just “fire and forget.” It acts as a direct parent process to the application and tracks it at the Linux kernel level using the following mechanisms:
1. PID Tracking (Process ID Ownership)
When systemd reads your unit file and executes ExecStart=/usr/bin/bash /home/pi/streamer.sh, the kernel assigns that running instance a unique Main PID (Process ID). Because systemd spawned that PID directly, it registers a low-level kernel event listener against it.
2. The exec Handoff
In your streamer.sh script, you make a crucial, highly optimized architectural choice: you launch the media player using the command exec mpv ....
-
Normally, a Bash script starts a child process, meaning Bash stays running in memory while
mpvruns inside it. -
The
execcommand tells the system to completely replace the running Bash script process in memory with thempvprogram itself, inheriting the exact same PID. -
Consequently, systemd’s direct kernel listener is now bound squarely to the raw
mpvbinary.
3. Catching the Exit Signal
If mpv encounters a critical network socket drop, a decoding segmentation fault, or someone forcefully kills it, the process terminates and sends an exit code back to its parent. Systemd catches this signal instantly. Because your unit file explicitly contains the rule Restart=always, systemd ignores why mpv closed and automatically triggers the recovery routine to launch it again after your defined 5-second cooldown (RestartSec=5).
Explain me what systemd and how teh background service works. Explain this systemd unit file
What is Systemd?
Systemd is the central management system (or “init system”) for modern Linux distributions, including the 32-bit Raspberry Pi OS Lite environment you are deploying. When your Raspberry Pi boots up, systemd is the very first process that starts (Process ID 1). Its job is to initialize the system components, manage the core hardware configurations, and automatically start background applications (called services or daemons) without requiring anyone to log in.
How a Background Service Works
When you run a command in the terminal manually, closing the terminal kills the program. A background service decouples the execution of your script from an active user session.
By encapsulating your execution script inside a systemd supervisor daemon, systemd takes complete ownership of running the process. It launches your script silently in the background, monitors its active memory buffers and health, and acts as an automated watchdog. If your Wi-Fi drops, the TV loses sync, or the stream encounters a transient power outage or network disruption that causes mpv to crash, systemd steps in immediately to restart the file from scratch.
Line-by-Line Explanation of Your Service File
This text is a Systemd Unit File. It acts as the instruction manual telling the operating system exactly how, when, and under what conditions to run your continuous rotation script.
[Unit] Section
This block defines the metadata for the service and its dependencies on other system components before it is allowed to launch.
-
Description=Reolink CCTV RTSP Dedicated StreamerA plain-text label for your service. When you check the status of your Pi or look at system logs, this description tells you what process you are looking at. -
After=network-online.targetInstructs systemd to wait to launch this service until after the operating system’s network stack has fully initialized. -
Wants=network-online.targetCreates a strong preference for an active network connection. Combined withAfter=, this ensures systemd attempts to establish your Wi-Fi connection before calling your media engine, preventing immediate failure loops upon boot.
[Service] Section
This block dictates the exact execution instructions, user permissions, and automated process recovery behaviors.
-
Type=simpleTells systemd that the service starts immediately upon launching the command defined below. It expects the process to run continuously in the foreground of its isolated background wrapper. -
User=raspberryDefines the specific Linux user account under which the script executes. Running as a standard user account rather than the powerfulrootadministrator protects the core system if the script encounters an error. (Note: Ensure your Pi actually has a user namedraspberryprovisioned, or change this to match your username). -
ExecStart=/usr/bin/bash /home/raspberry/streamer.shThe absolute core command of the service. It calls the raw Bash interpreter (/usr/bin/bash) and tells it to run your specific procedural script located at/home/raspberry/streamer.sh. -
Restart=alwaysThe automated process recovery directive. Ifmpvcleanly exits after completing a stream segment, crashes due to network disconnects, or is terminated unexpectedly, systemd will always attempt to relaunch it. -
RestartSec=5Tells the supervisor daemon to pause for exactly 5 seconds before attempting to restart a dead service. This crucial buffer prevents the Pi’s CPU from spinning out of control in an infinite restart loop if a persistent error occurs. -
StandardOutput=journal&StandardError=journalRoutes all standard text output and error logs generated bympvdirectly into the central daemon journal. Because you are in a headless environment without a desktop terminal to watch, routing to the journal allows you to capture stream transport drops or authentication failures in real-time by running system commands likejournalctl.
[Install] Section
This block tells systemd how to link the service into the automated boot sequence.
WantedBy=multi-user.targetIn a headless Linux environment,multi-user.targetis the standard operational state where all core system components are loaded, networking is active, and the system is ready for multiple background processes. By enabling this file, systemd creates a permanent symlink so your streamer service boots automatically the moment the Pi reaches this operational state.