Use systemd-run as an alternative for screen

Posted . Visible to the public.

You might use screen or tmux to run a temporary command on a server which continues to run after the SSH session is closed.

Consider systemd-run Show archive.org snapshot as alternative. It will turn every command in a systemd service unit:

# Run `openssl speed` as unit run-benchmark.service
$ sudo systemd-run --unit=run-benchmark openssl speed

# Query the current status
$ systemctl status run-benchmark.service
● run-benchmark.service - /usr/bin/openssl speed
   Loaded: loaded (/run/systemd/transient/run-benchmark.service; transient)
Transient: yes
   Active: active (running) since Wed 2022-11-23 09:25:09 CET; 7min ago
 Main PID: 18344 (openssl)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/run-benchmark.service
           └─18344 /usr/bin/openssl speed


# Get the logs
$ sudo journalctl -u run-benchmark.service
-- Logs begin at Mon 2022-03-07 13:45:18 CET, end at Wed 2022-11-23 09:31:33 CET. --
Nov 23 09:25:09 server systemd[1]: Started /usr/bin/openssl speed.
Nov 23 09:25:12 server openssl[18344]: Doing md4 for 3s on 16 size blocks: 12952588 md4's in 3.00s
Nov 23 09:25:15 server openssl[18344]: Doing md4 for 3s on 64 size blocks: 9936296 md4's in 3.00s
Nov 23 09:25:18 server openssl[18344]: Doing md4 for 3s on 256 size blocks: 5475381 md4's in 3.00s
Nov 23 09:25:21 server openssl[18344]: Doing md4 for 3s on 1024 size blocks: 2126597 md4's in 3.00s
Nov 23 09:25:24 server openssl[18344]: Doing md4 for 3s on 8192 size blocks: 311796 md4's in 3.00s
Nov 23 09:25:27 server openssl[18344]: Doing md4 for 3s on 16384 size blocks: 156100 md4's in 3.00s
Nov 23 09:25:30 server openssl[18344]: Doing md5 for 3s on 16 size blocks: 17646219 md5's in 3.00s
Nov 23 09:25:33 server openssl[18344]: Doing md5 for 3s on 64 size blocks: 10281058 md5's in 3.00s

# Stop the command
$ sudo systemctl stop run-benchmark
Andreas Vöst
Last edit
Andreas Vöst
License
Source code in this card is licensed under the MIT License.