Section 13.21

systemd service versus scope

Full name: systemd (the name comes from system daemon, a daemon being a program that runs in the background) service unit versus scope unit.

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.

  1. A unit file, or a request at run time that creates a transient unit, gives the command line in ExecStart= and the start type in Type=.

  2. 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.

  3. The start type decides when the start counts as complete: for simple, as soon as the process exists; for exec, once the program file has been executed; for notify, when the program sends a ready message; for forking, when the started process exits, leaving its child as the main process; for oneshot, only when the main process has exited, so for this type a completed start means completed work.

  4. When the main process ends, systemd records how. The property ExecMainCode says whether it exited or was killed by a signal, a kernel notice that can end a process, and ExecMainStatus holds the exit status or the signal number. Exit status 0, any status listed in SuccessExitStatus=, and the termination signals SIGHUP, SIGINT, SIGTERM and SIGPIPE count as success. Anything else puts the unit in the failed state, with a result such as exit-code or signal.

  5. systemd then stops the unit. With the default KillMode=control-group, every process still in the unit’s cgroup receives SIGTERM, 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.

  1. 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.

  2. 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.

  3. The scope is active while at least one process is in it. All processes in a scope are equal; none is the main process.

  4. 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.

  5. 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.

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 ss.

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 ss 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].