Introducing the “Sovereign Snippets” Series: Most of our time is spent in complex configurations, but the real power of a Linux system often hides in the short, punchy commands we use every day to maintain control over our hardware. I’m starting this series to document the specific “one-liners” I use to audit, secure, and manage my systems. No fluff—just functional tools for those who prefer the CLI over a GUI.
The Burden of Permanence
In Snippet #8, we talked about using disown to save a doomed SSH session. But what if you are planning ahead?
You need to hash a massive 2TB disk image, run a heavy database migration, or execute an aggressive find command. You know it will take hours, and you know it will thrash the server’s CPU and RAM.
You have two bad options. You can throw it into the background with nohup command &, which strips away your ability to monitor its resource usage or easily read its logs. Or, you can spend ten minutes lovingly handcrafting a permanent .service unit file in /etc/systemd/system/, execute the job, and then inevitably forget to delete the file, accidentally adopting yet another piece of permanent infrastructure.
There is a third option. You can use systemd-run to spin up a “transient” service. It gives your one-off command all the heavy-duty supervision, logging, and cgroup resource limits of a permanent daemon, and then it automatically deletes itself the moment the job finishes.
The Restricted Background Job
Let’s say we need to calculate the SHA256 hash of a massive disk image, but we don’t want it to starve the CPU or consume all the system memory while we are running other web services.
Run this:
sudo systemd-run \
--unit=snippet-hash \
--collect \
--property=CPUQuota=50% \
--property=MemoryHigh=512M \
--property=MemoryMax=768M \
/usr/bin/sha256sum /srv/archive/disk-image.rawBreaking Down the Flags
This command wraps your simple sha256sum executable in an invisible, temporary Linux container (cgroup).
--unit=snippet-hash: We give the transient job a recognizable name.--collect: This is the magic cleanup flag. It tells systemd to completely unload the unit from memory the moment it finishes. No leftover state, no clutter.--property=CPUQuota=50%: We throttle the job. Note that this means 50% of one CPU core, not half of the entire machine.MemoryHighvsMemoryMax: We set a soft limit (MemoryHigh=512M), which forces the kernel to aggressively throttle the process and reclaim memory if it crosses the line. We also set a hard emergency ceiling (MemoryMax=768M). If the job breaches this ceiling, the kernel’s Out-Of-Memory (OOM) killer will execute it immediately, protecting the rest of your server.
Because this is a systemd unit, its standard output is automatically wired into the journal. You can safely disconnect from SSH, go to lunch, come back, and monitor the progress at any time:
journalctl -fu snippet-hash.service(Even after --collect destroys the unit, the journal entries remain permanently archived for your review).
The Context: Scopes and Time Bombs
systemd-run isn’t just for background daemons. It is a multi-tool for process isolation.
1. The Interactive Sandbox (--scope)
If you want to run a heavy command interactively (so you still see the output on your screen right now) but you still want to trap it inside a resource-limited cgroup, use a scope:
sudo systemd-run \
--scope \
--property=CPUQuota=75% \
--property=MemoryMax=1G \
/usr/bin/find /srv -xdev -type f -size +1G -printYou keep your shell prompt, but the find command is mathematically forbidden from consuming more than 1GB of RAM.
2. The Disposable Timer
Forget writing cron jobs for things you only need to happen once. You can schedule a transient timer that fires once and then evaporates.
sudo systemd-run \
--unit=snippet-reminder \
--on-active=20m \
--timer-property=AccuracySec=1m \
/usr/bin/logger -t sovereign-snippet "The maintenance window is over"You can verify this ticking time bomb is active by running systemctl list-timers snippet-reminder.timer.
The Caveats (The Tax on Convenience)
Because you are handing execution over to the init system, you lose your comfortable interactive-shell habitat.
- Absolute Paths are Mandatory: You cannot use
~for your home directory, and you should always use absolute paths for executables (e.g.,/usr/bin/sha256sum). - The Shell Pipe Tax: Shell operators like pipes (
|), redirects (>), and wildcards (*) are not interpreted by systemd. If your one-off job requires complex piping, you must explicitly wrap it in a shell executable:
sudo systemd-run --collect /bin/sh -c 'cat /var/log/syslog | grep error > /var/tmp/result'This reintroduces shell-quoting hazards, but as always in Linux, convenience must eventually collect its tax.
Over to you: Have you been cluttering your system with one-off shell scripts, or have you embraced systemd-run to keep your environment strictly governed? Let me know in the comments.