Use the systemd-run command to run a program in a transient scope or service unit. This allows you to set limits on resources consumed by the service during its runtime. The transient unit is removed automatically as soon as the service is stopped.
When a command is run as a transient service unit, the command is started and managed by systemd like any other service and is included in the output of the systemctl command like any other unit. The command runs in a clean and detached execution environment. systemd-run starts the service asynchronously in the background and immediately returns.
When a command is run as a transient scope unit, the command is started directly by systemd-run and inherits the execution environment of the caller. The command is managed by systemd and is included in the output of systemctl. Execution is synchronous, and execution returns only when the command finished.
Syntax for the systemd-run command follows:
systemd-run –unit=<name> –scope –slice=<slice_name> <command>
If –unit is not specified, the unit name is automatically generated. A transient service unit is created by default. Include the optional –scope parameter to create a transient scope unit. Use the –slice parameter to specify a new or existing slice. Otherwise, the transient service or scope is created in system.slice. Provide the command to run as the final argument.
Additional systemd-run options include the following:
- –description – Create a description of the unit.
- –remain-after-exit – Keep the service until explicitly stopped. This is useful to collect runtime information about the service after it finishes running.
- –help – Display usage with additional options.
The following example runs the dd command as a transient service:
# systemd-run dd if=/dev/zero of=/dev/null bs=1024
Running as unit run-8928.service.
The “Running as …” message displays to confirm the service starts successfully. The unit name, run-8928.service, is automatically generated because the –unit=name option is not specified.
The find command displays the location of the run-8928.service file:
# find / -name run-8928.service
Note that the service is placed in the system.slice hierarchy by default. The service is included as output of the systemctl command:
run-8928.service loaded active running /bin/dd if=/dev/zero …
You can use the top utility to show that the dd command is using much of the CPU and memory resources. You can also use the systemd-cgtop command, which shows top cgroups by their resource usage:
Path Tasks %CPU Memory Input/s …
/ 159 100.0 744.2M –
/system.slice 48 86.1 5.7M –
/system.slice/run-8928.service 1 86.1 – –
Transient cgroups are released automatically as soon as the processes they contain finish. You can also use the systemctl stop command to stop the service:
# systemctl stop run-8928