In this lesson, you will learn how to use systemd-run to create "transient" units—temporary services that exist only in memory and disappear once they finish their task.
Why use transient units?
Typically, systemd services are defined by static files on your disk (like /etc/systemd/system/myservice.service). However, sometimes you need to run a task just once, or you want to offload a process to the background without the overhead of creating, installing, and enabling a full unit file. This is where transient units come in.
A transient unit is a service created dynamically via the system bus. It is managed by systemd just like any other service, meaning it respects cgroup limits, logging, and dependency tracking, but it is never written to the disk.
Using systemd-run
The primary tool to create these units is systemd-run. By default, when you execute a command through it, systemd creates a transient service unit on the fly, runs your command, and cleans up the unit automatically once the process exits.
Try running a simple sleep command as a transient service:
systemd-run --unit=my-transient-task sleep 60
When you run this, systemd registers a unit named my-transient-task.service. You can monitor its status with systemctl status my-transient-task even though no file exists for it on your hard drive. Once the sleep 60 command completes, the unit will transition to an "inactive" state and eventually vanish from systemd’s memory entirely.
Controlling the Lifecycle
Because these are standard units, you can apply standard service properties to them. For example, if you want a transient task to be remembered for a short period after it finishes (so you can inspect its exit code), you can use the --remain-after-exit flag. You can also specify properties like memory limits using --property:
systemd-run --user --property=MemoryMax=50M bash -c "stress --cpu 1"
This command creates a transient unit that forces the process to stay within a 50MB memory ceiling, managed by the same supervisor that handles your permanent system daemons.
Key Takeaway: Transient units provide the power of systemd’s management and monitoring for ad-hoc, temporary tasks without the clutter of persistent configuration files.