$linuxjunkies
>

systemd-run(1)

Run a program in a new systemd unit, useful for setting resource limits, cgroups, and tracking processes.

UbuntuDebianFedoraArch

Synopsis

systemd-run [OPTIONS] [COMMAND] [ARGUMENTS...]

Description

systemd-run launches a program in a transient systemd unit, which allows you to apply systemd settings like resource limits, CPU/memory constraints, and service properties to arbitrary commands. This is useful for temporary one-off processes that need special isolation or accounting.

The unit is automatically cleaned up when the process exits. You can run programs in the system instance or user instance, as a service, scope, or timer unit.

Common options

FlagWhat it does
-u, --unit=NAMESet the name of the transient unit (default: auto-generated)
-p, --property=NAME=VALUESet systemd unit properties like MemoryLimit=100M, CPUQuota=50%
--scopeCreate a scope unit instead of a service (default is service)
--userRun in the user instance instead of the system instance
-d, --working-directory=PATHSet working directory for the process
-E, --setenv=NAME=VALUESet environment variables for the process
--no-waitStart the unit asynchronously and return immediately
-S, --slice=SLICEPlace unit in the specified slice (e.g., user-background.slice)
--ptyAllocate a pseudo-terminal for the process
-q, --quietSuppress status output

Examples

Run htop in a user scope unit, easy to track and kill the process tree

systemd-run --user --scope htop

Run myapp with 100MB memory limit and 50% CPU quota

systemd-run -p MemoryLimit=100M -p CPUQuota=50% myapp --option

Run script.sh with working directory /tmp and custom environment variable

systemd-run -d /tmp --setenv=VAR=value ./script.sh

Start an async user backup job and return immediately without waiting

systemd-run --user --no-wait -u backup-job /usr/local/bin/backup.sh

Run Python script in a named unit with memory accounting enabled

systemd-run -u heavy-task -p MemoryAccounting=yes python3 analysis.py

Start an interactive bash shell in a user instance with terminal

systemd-run --pty --user bash

Run wget in the background.slice for lower priority execution

systemd-run -S background.slice wget https://example.com/file.tar.gz

Related commands