Running One-Off Tasks Safely with systemd-run

Every Linux user has started a long-running command over SSH, only to lose it when the connection drops. The usual fixes—nohup, screen, or a trailing &—work in a pinch, but they leave you with limited visibility, no resource controls, and manual cleanup. On modern Linux systems, systemd-run offers a cleaner alternative: it creates transient units for commands you need to run once, giving you journald logging, status tracking, timers, and cgroup limits without writing a permanent unit file.

Why Use Transient Units

Transient units are regular systemd units that exist only in memory. You get the same supervision model as services you define in /etc/systemd/system, but without creating files that linger after the job is done.

ApproachSurvives logoutCentralized logsResource limitsSchedulingCleanup
command &NoTerminal onlyNoNoManual
nohup command &Yesnohup.outNoNoManual
at / cronYesVaries by setupLimitedYesEdit config
systemd-runYesjournaldYes (cgroups)Yes (transient timers)Automatic with --collect

systemd-run is especially useful for maintenance tasks, ad-hoc backups, data imports, and any command you want to observe or constrain.

Check Your Environment

Most mainstream distributions ship with systemd. Confirm it is available and note the version:

1systemctl --version
2systemd --version

You need root privileges for system-level units. For personal tasks, --user runs the unit in your user session and does not require sudo.

1# Run a quick command in the user manager
2systemd-run --user --wait --collect --pipe \
3  /bin/bash -c 'echo "Running as $(whoami) on $(hostname)"'

If the user manager is not running in a non-interactive SSH session, enable lingering:

1sudo loginctl enable-linger "$USER"

Run a Command as a Transient Service

The simplest form creates a background service and returns immediately:

1sudo systemd-run --unit=quick-backup \
2  --description="One-off home directory backup" \
3  --remain-after-exit \
4  /usr/bin/tar czf /var/backups/home.tar.gz /home

Key options in this example:

  • --unit=quick-backup gives the unit a predictable name instead of a generated one.
  • --description=... adds human-readable context to systemctl status.
  • --remain-after-exit keeps the service in an active (exited) state after the command finishes, making it easy to inspect.

Inspect the result with standard systemd tools:

1systemctl status quick-backup.service
2journalctl -u quick-backup.service -n 50 --no-pager

When you no longer need the unit, stop it and clear any failed state:

1sudo systemctl stop quick-backup.service
2sudo systemctl reset-failed quick-backup.service

Wait for Completion and Capture Output

For scripts or interactive administration, you often want the command to behave like a normal foreground process while still getting systemd supervision. Use --wait, --collect, and --pipe together:

1sudo systemd-run --wait --collect --pipe \
2  --unit=check-disk \
3  /usr/bin/df -h /
  • --wait blocks until the service exits and returns its exit code.
  • --collect unloads the unit automatically after it finishes, even if it failed.
  • --pipe connects the service's standard input, output, and error to your terminal.

This pattern is ideal for wrapping a command in resource limits without changing how you invoke it:

1sudo systemd-run --wait --collect --pipe \
2  -p WorkingDirectory=/srv/app \
3  -p Environment="APP_ENV=production" \
4  /usr/local/bin/run-import.sh

If you need a pseudo-terminal for programs that expect one, replace --pipe with --pty.

Schedule One-Off Jobs with Transient Timers

systemd-run can also create transient timers. This is useful when you want a job to run later without editing crontab or creating timer files.

Run After a Delay

1sudo systemd-run --on-active=30min \
2  --unit=cleanup-downloads \
3  /usr/bin/find /home/alice/Downloads -type f -mtime +30 -delete

The command runs 30 minutes after the timer is created. You can use time spans like 90s, 2h, or 1d.

Run at a Specific Time

1sudo systemd-run --on-calendar="2026-11-24 03:00:00" \
2  --unit=nightly-report \
3  /usr/local/bin/generate-report.sh

For recurring calendar expressions, systemd's calendar syntax applies. For example, *-*-* 03:00:00 means every day at 03:00.

Inspect scheduled transient timers:

1systemctl list-timers --all --no-pager
2systemctl status nightly-report.timer
3journalctl -u nightly-report.service --no-pager

The timer unit triggers a service unit with the same base name. When debugging, check both the .timer and .service units.

Apply Resource Limits and Sandboxing

One of the strongest reasons to use systemd-run is access to systemd's resource and sandboxing properties. These map to cgroup settings and kernel namespaces.

Resource Limits

1sudo systemd-run --wait --collect --pipe \
2  -p MemoryMax=512M \
3  -p CPUQuota=80% \
4  -p IOWeight=50 \
5  /usr/local/bin/heavy-job.sh
PropertyEffectExample
MemoryMax=Hard memory ceilingMemoryMax=512M
CPUQuota=CPU time limit relative to one coreCPUQuota=80%
IOWeight=Relative I/O priorityIOWeight=50
TasksMax=Maximum number of tasks/processesTasksMax=100

If a process exceeds MemoryMax, the kernel's OOM killer may terminate it inside the unit's cgroup. Check the journal for OOM messages when a job disappears unexpectedly.

Basic Sandboxing

For tasks that do not need full system access, add protective properties:

1sudo systemd-run --wait --collect --pipe \
2  -p PrivateTmp=yes \
3  -p ProtectSystem=strict \
4  -p ProtectHome=read-only \
5  -p NoNewPrivileges=yes \
6  -p ReadWritePaths=/var/lib/myapp \
7  /usr/local/bin/untrusted-task.sh

These settings reduce blast radius:

  • PrivateTmp=yes gives the process its own /tmp.
  • ProtectSystem=strict makes most of the filesystem read-only.
  • ProtectHome=read-only prevents writes to user home directories.
  • NoNewPrivileges=yes blocks privilege escalation through setuid binaries.
  • ReadWritePaths= reopens specific paths for writes.

Test sandboxed commands carefully. Overly restrictive settings can cause legitimate failures, such as inability to write logs or temporary files.

Troubleshooting Common Issues

When a transient unit does not behave as expected, start with these checks:

1# Show recent logs for the unit
2journalctl -u quick-backup.service -e --no-pager
3
4# Inspect the generated unit properties
5systemctl show quick-backup.service
6
7# List failed units
8systemctl --failed --no-pager

Common problems include:

  • Unit name already exists: transient unit names must be unique while loaded. Pick a new name or stop the old unit.
  • Command not found: use absolute paths inside systemd-run, because the execution environment may differ from your shell.
  • Permission errors: system units run as root by default; add -p User=alice -p Group=alice when the job should run as a non-root account.
  • Timer never fires: verify the system clock, timezone, and the timer status with systemctl list-timers --all.

Quick Reference

GoalCommand pattern
Run in backgroundsudo systemd-run --unit=name /path/to/cmd
Run and waitsudo systemd-run --wait --collect --pipe /path/to/cmd
Run as current usersystemd-run --user --wait --collect --pipe /path/to/cmd
Run after delaysudo systemd-run --on-active=30min /path/to/cmd
Run at specific timesudo systemd-run --on-calendar="..." /path/to/cmd
Limit memorysudo systemd-run -p MemoryMax=512M /path/to/cmd

systemd-run does not replace full unit files for long-lived services, but for one-off jobs it hits a sweet spot: you get the observability and control of systemd with the immediacy of a shell command. The next time you reach for nohup, consider making the job a transient unit instead—you will have logs, limits, and a clear lifecycle from the start.

This article was generated with AI assistance and reviewed by a human. Corrections are welcome.

[ COMMENTS ]

© 2026 AI Assistant · Code Daily · Daily Articles_