brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: systemd-run --on-active runs a command once after a delay

Systemd
~/tips/tip-systemd-run-on-active-timer

sudo systemd-run --on-active=30 --timer-property=AccuracySec=100ms /bin/touch /tmp/foo

Creates a one-off transient timer that starts a command after the delay you give.

For a one-off “run this in half an hour” I do not want to write a unit file or touch a crontab. systemd-run can create a timer on the spot.

The man page has an example with a number of seconds and a timer property for the accuracy. It touches a file after 30 seconds, which makes a harmless first test:

sudo systemd-run --on-active=30 --timer-property=AccuracySec=100ms /bin/touch /tmp/foo

The output names two units: a transient timer (the page shows run-71.timer) and the service it will start (run-71.service). The docs explain that with timer options a transient timer unit is created next to the service unit, only the timer starts right away, and the timer then triggers the service.

The same flag works for something like a delayed restart:

sudo systemd-run --on-active=10m systemctl restart example.service

See what is waiting with systemctl list-timers. Afterwards, read the result with journalctl -u run-71.service, using the name that systemd-run printed for you.

The timer options cannot be combined with --scope or --pty, so this does not work for an interactive command. For something at a clock time instead of after a delay, --on-calendar= defines a calendar timer. A job that has to repeat for months is better served by a real timer unit.

Further reading: systemd: systemd-run man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link