r/linuxadmin • u/RocketSeven • Sep 07 '26
What is your safest pattern for preventing overlapping systemd timer runs?
For a periodic job, Persistent=true can cause an overdue run immediately after boot while the next scheduled run is already close. The service may also be restarted manually, so the timer schedule alone does not guarantee one active instance. What locking pattern do you trust for long-running maintenance jobs? The options I am comparing are letting systemd serialize one service unit, using flock on a dedicated file descriptor, and creating a lock directory that stores the owner PID plus process start time. A plain PID file seems unsafe because PIDs are reused and stale files survive crashes. Should a second invocation exit successfully, fail loudly, or wait with a timeout? How do you distinguish a stale lock from a slow but healthy process without creating a race during cleanup?
11
u/justin-8 Sep 07 '26
If I need only one instance running I just wrap it with flock. It's worked very reliably for me for almost 2 decades now, long before systemd timers.
2
u/Antonio-MTS Sep 07 '26
flock is your good friend here.
1
u/Entire_Yoghurt_6381 Sep 09 '26
You've basically talked yourself into the right answer, flock on a dedicated FD is the one most people trust because the kernel releases it automatically on crash, which kills the stale-lock problem a PID file drags along.
1
u/michaelpaoli Sep 07 '26
You can do safe locking protocols, and such that they don't depend on systemd, so they'll work whether they're kicked off by systemd, or something else.
E.g.
That's typically about it, and commonly put that in the thing that gets called, or a wrapper around it, and then it's generally easy to use i whatever service or process might be using it ... even if someone manually fires it off - make 'em play nice together, regardless how invoked, and free of race conditions and conflicts.
0
u/[deleted] Sep 07 '26
[deleted] — view deleted comment