Definition
systemd is the program that starts and supervises the other programs of most Linux systems, managing them as named objects called units. A service is a unit for a program that systemd itself starts, watches and stops, so systemd knows the main process and records how it ended. A scope is a unit for processes that another program has already started; systemd only tracks and limits them. A service ends by rules about its main process; a scope ends when its last process has gone, whatever the exit statuses.
The idea
A measurement harness must run a benchmark program with limited memory, learn when the program has finished and learn whether it succeeded. On a Linux system with systemd it has two ways to do this. It can ask systemd to start the program as a service, with a command such as systemd-run bench; systemd then becomes the program’s parent, and systemd-run returns as soon as the program has begun to run. Or it can have the program started inside the harness’s own process tree and placed in a scope, which systemd-run --scope bench does: systemd-run asks systemd for a scope that contains its own process and then replaces itself with the program, so the program runs as a child of the harness, and the call returns only when the program finishes. The two ways answer “when is it finished?” and “did it succeed?” from different places.
Hiring through an agency versus registering one’s own crew is a fair picture. An agency, like systemd running a service, hires the worker, supervises the job and reports whether it was done. Building management, like systemd holding a scope, only records who is in the building and enforces the house rules; only whoever hired the crew hears how the job went. The picture stops being accurate in one respect: systemd can still mark a scope failed for reasons of its own, such as its processes exceeding a resource limit.
How it works
systemd puts the processes of each unit into a cgroup §13.20 of its own, a kernel group that every new process joins when its creator is a member. The unit’s resource limits apply to that cgroup. Units are arranged under slices, units that group other units for resource control, such as system.slice for system services.
A service unit follows this course.
A unit file, or a request at run time that creates a transient unit, gives the command line in
ExecStart=and the start type inType=.systemd creates a process in the unit’s cgroup and runs the command in it. systemd is that process’s parent, so it receives the exit status §13.24.
The start type decides when the start counts as complete: for
simple, as soon as the process exists; forexec, once the program file has been executed; fornotify, when the program sends a ready message; forforking, when the started process exits, leaving its child as the main process; foroneshot, only when the main process has exited, so for this type a completed start means completed work.When the main process ends, systemd records how. The property
ExecMainCodesays whether it exited or was killed by a signal, a kernel notice that can end a process, andExecMainStatusholds the exit status or the signal number. Exit status 0, any status listed inSuccessExitStatus=, and the termination signalsSIGHUP,SIGINT,SIGTERMandSIGPIPEcount as success. Anything else puts the unit in the failed state, with a result such asexit-codeorsignal.systemd then stops the unit. With the default
KillMode=control-group, every process still in the unit’s cgroup receivesSIGTERM, a request to terminate, and after the stop timeout §9.34, 90 seconds by default,SIGKILL, which cannot be refused.
A scope unit follows a different course.
A program that has already started processes asks systemd, through systemd’s programming interface on the system message bus, to create a scope, and gives the process identifier (PID) of each of those processes. Scopes have no unit files; they exist only at run time.
systemd moves the processes into the scope’s cgroup. It starts nothing itself. The processes stay children of the program that created them, and that program collects their exit statuses.
The scope is active while at least one process is in it. All processes in a scope are equal; none is the main process.
When the last process has gone, the scope stops. The exit statuses of its processes do not affect its state. A scope can still fail, for example when its processes exhaust a resource limit or do not stop in time when the scope is stopped.
Stopping a scope, for example with
systemctl stop, terminates all its processes.
Login sessions are scopes, and so are containers and virtual machines §2.3 registered with systemd.
The command systemd-run creates transient units. Without options it creates a service and returns once the command has begun to run, so its own exit status 0 means only that the start succeeded. With --wait it waits until the service has ended and returns the service’s exit status, with 0 for every ending that systemd counts as clean. With --scope it creates a scope that contains its own process and then replaces itself with the command through the exec system call, which makes a process run a different program; the command therefore keeps the process identifier and the parent of systemd-run, and the call returns only when the command finishes.
Worked example
A worker program exits with status 7 after 4 seconds. Before exiting it starts a helper, which would run until the 9-second mark. Here are the two ways to run it, starting at time 0.
As a service, with
systemd-run --unit=bench worker.systemd-runexits with status 0 within a fraction of a second, as soon as the worker has begun to run. At 4 s the worker, the main process, exits with 7. That is not a success status, so systemd marksbench.servicefailed, with resultexit-codeandExecMainStatus7, and stops the unit: the helper receivesSIGTERMat 4 s and ends then, unless it ignores that signal. A harness that kept only the 0 fromsystemd-runrecords success; the failure is in the unit’s record. With--wait,systemd-runwould have returned 7 at 4 s.As a scope, with the harness starting the worker as its own child and asking systemd to place it in a new scope. At 4 s the worker exits with 7, and the harness’s wait call returns 7. The scope stays active, because the helper is still in it. At 9 s the helper exits, the scope has no process left, and it becomes inactive without any failure. The 7 exists only in the harness’s own record.
The service recorded the main process’s status at 4 s and stopped the remaining work then; the scope recorded only that its last process ended at 9 s.
Stated precisely
Keep five events apart.
- E1
-
The start was requested.
- E2
-
The start counts as complete under
Type=. - E3
-
The main process ended, with exit status or signal .
- E4
-
The unit’s cgroup holds no live process.
- E5
-
The unit reached its final state, inactive or failed.
For a service with default settings, systemd sees all five events. It sets E5 to failed if and only if is not a success value or systemd detected a failure of its own, such as a timeout. The success values are exit status 0, the statuses listed in SuccessExitStatus=, and the signals SIGHUP, SIGINT, SIGTERM and SIGPIPE. After E3, systemd stops the unit and so forces E4 within the stop timeout. A scope has no main process, so E2 and E3 do not exist for it: systemd sees E1 and E4, and reaches E5 when E4 happens. E5 is failed only for a failure of systemd’s own, such as an exhausted resource limit or a stop timeout, never because of an exit status, which only the parent process receives. No single unit state records all five events.
Limits and common mistakes
Settings change these rules. RemainAfterExit=yes keeps a service active after its process has ended, and Restart= can start the program again, so an active unit can be a second run. Read the unit’s actual settings, for example with systemctl show, before interpreting its state.
A transient service that ends cleanly is unloaded soon afterwards, and its result can no longer be queried; a failed one stays until it is reset, unless systemd-run was given --collect. Record the result when the unit ends, which --wait does.
An inactive scope says that its processes are gone, not that any of them succeeded. A successful service says that its main process reported success, not that the work’s output is complete or valid.
systemd counts SIGTERM as a clean ending. A service whose main process was terminated by that signal is therefore not failed, although its work was cut short.
Both kinds of unit cover processes on one machine. Work handed to another service, a command still being carried out by a device, and processes with administrator rights that moved themselves out of the unit’s cgroup all lie outside the unit.
Questions answered
Who starts the process and who determines when the unit is complete?
For a service unit, systemd, the Linux program that manages other programs as units, starts the process itself and also determines completion; for a scope unit, another program starts the processes, and the scope is complete when its last process has gone. In a service, systemd is the parent of the main process, so it receives the exit status. The Type= setting decides when the start counts as complete, and the unit ends when the main process ends: it is marked failed unless the status is a success value, and any processes left in the unit’s cgroup are then terminated.
In a scope, the program that started the processes remains their parent and is the only one that receives their exit statuses. systemd only tracks the processes and limits their resources; the scope’s state ignores how they exited. A record that a run finished should therefore keep both the unit’s final state and result and, for a scope, the exit statuses that the parent collected.
Related entries: cgroup (§13.20), Process closure (§13.23), Exit status (§13.24). Further reading: [32, 133, 134].