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: RuntimeMaxSec= stops a systemd service that runs longer than it should

Systemd
~/tips/tip-systemd-runtimemaxsec-limit-runtime

[Service]

Terminates a service and marks it failed when it exceeds a maximum run time.

Some jobs are supposed to take ten minutes and then, once in a while, hang for a day. A timeout on the start does not help with that. systemd has a limit for the total run time.

[Service]
RuntimeMaxSec=2h

The docs for systemd.service(5) say that a service that runs longer than this is terminated and put into a failure state. The default is infinity, which means no limit. The option was added in systemd 229.

A long-running worker that sometimes hangs is a good fit. The hung run is ended and the unit shows up in systemctl --failed, so you notice it.

You can spread the limit a little with RuntimeRandomizedExtraSec=, added in version 250. It adds a random extra time between 0 and the value you give, and it does nothing unless RuntimeMaxSec= is set.

The caveat is in the docs as well: RuntimeMaxSec= has no effect on Type=oneshot services, because they exit right after activation. For those you limit the activation with TimeoutStartSec= instead. Services of Type=notify can extend the runtime by sending EXTEND_TIMEOUT_USEC= until they report STOPPING=1 or exit.

Also remember that the stop itself follows TimeoutStopSec=, so a service that ignores SIGTERM is still killed with SIGKILL after that time.

Further reading: systemd.service: RuntimeMaxSec=

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

Link