Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

systemd-run

Run a command as a temporary (transient) systemd service or scope — now, later with a timer, or with resource limits.

Level
Everyday Linux
Group
Scheduling
Needs root
For changes
Package
systemd

What it does

systemd-run COMMAND starts the command as a new service unit (like run-u123.service). It runs in the background, is managed by systemd, and its output goes to the journal. No unit file is needed.

With timer options (--on-calendar, --on-active) it works like at with logging. With -p you set unit properties, for example memory or CPU limits. --scope runs the command in the foreground in its own cgroup.

How to write it

systemd-run [OPTION]... COMMAND [ARGUMENT]...

Options 35

Most used

Unit

-u, --unit=NAME
The name of the unit.
-p, --property=NAME=VALUE
Set a unit property, like MemoryMax=500M.
--description=TEXT
Description of the unit.
--slice=SLICE
Put the unit in this slice.
--slice-inherit
Use the slice of the caller.
--scope
Run as a scope in the foreground (not a service).
--service-type=TYPE
Type= of the service (simple, oneshot …).
-r, --remain-after-exit
Keep the unit after the command ends.
--send-sighup
Send SIGHUP after SIGTERM when stopping.
-G, --collect
Remove the unit also after a failure.

Run as / environment

--uid=USER, --gid=GROUP
Run as this user and group.
--nice=N
Nice value.
--working-directory=DIR
The working folder.
-d, --same-dir
Use the current folder.
-E, --setenv=NAME[=VALUE]
Set an environment variable.
--expand-environment=BOOL
Expand variables in the command.

Input and output

-t, --pty
Connect to the terminal (interactive).
-P, --pipe
Connect stdin/stdout/stderr to the caller.
-S, --shell
Start an interactive shell as a service.
-q, --quiet
Less output.
--wait
Wait until the command ends and show its result.
--no-block
Do not wait for the start.

Timers and triggers

--on-active=TIME
Run this long from now.
--on-boot=TIME, --on-startup=TIME
Time after boot / after systemd start.
--on-unit-active=TIME, --on-unit-inactive=TIME
Relative to the unit's last activity.
--on-calendar=SPEC
Run at calendar times, like *-*-* 02:00.
--on-clock-change, --on-timezone-change
Run when the clock or time zone changes.
--timer-property=, --path-property=, --socket-property=
Properties for the timer, path or socket unit.

Other

--user
Use the user manager.
--system
Use the system manager (default).
--no-ask-password
No password questions.
-H, --host=HOST
Remote host over SSH.
-M, --machine=NAME
A container.
-h, --help
Show help.
--version
Show the version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Run a command as a background service

    systemd-run --unit=reindex /usr/local/bin/reindex.sh
    Running as unit: reindex.service

    Survives logout; follow it with journalctl -u reindex -f.

  2. 2Run once in 30 minutes

    systemd-run --on-active=30m /usr/bin/systemctl restart httpd

    Like at, but logged in the journal.

  3. 3Run every day at 02:00 (until reboot)

    systemd-run --on-calendar='*-*-* 02:00' --unit=nightly /usr/local/bin/report.sh

    A quick timer for a test; write unit files for permanent ones.

  4. 4Limit memory and CPU of a command

    systemd-run --scope -p MemoryMax=500M -p CPUQuota=50% tar -czf /tmp/big.tgz /data

    The command cannot take the whole server.

  5. 5Run as another user

    systemd-run --uid=lisa --pty --wait /usr/bin/whoami
    lisa

    Clean environment, like a service.

  6. 6See the result of a command

    systemd-run --wait --pipe /usr/bin/false

    Shows the exit code and run time.

  7. 7Your own long task without root

    systemd-run --user --unit=longtask ./long-task.sh
    journalctl --user -u longtask -f

    Better than nohup.

  8. 8Test hardening settings on a command (DevSecOps)

    systemd-run --pty -p ProtectSystem=strict -p PrivateTmp=yes /usr/bin/bash

    Try what a service could do with these settings.

  9. 9List and stop transient units

    systemctl list-units 'run-*'
    systemctl stop reindex

    They disappear after they end.

  10. 10Check running timers made by systemd-run

    systemctl list-timers

    Transient timers show up like normal ones.

Exit codes

0 :: Done (with --wait: the exit status of the command). 1 :: Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man systemd-run