TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Prevent duplicate cron job running

24 pointsby the2ndfloorguyover 3 years ago

13 comments

champtarover 3 years ago
You can also just use systemd timers :)
评论 #28413101 未加载
评论 #28412897 未加载
评论 #28412579 未加载
rjh29over 3 years ago
We used to run crons using a custom wrapper which handled locking, logging, state reporting and enable/disable via puppet. However, systemd timer units basically do all of this for you, so I'd recommend that instead.
评论 #28415271 未加载
jbboehrover 3 years ago
If you use consul, you can use `consul lock` [0]. Allows you to have the cron configured on multiple servers too.<p>[0]: <a href="https:&#x2F;&#x2F;www.consul.io&#x2F;commands&#x2F;lock" rel="nofollow">https:&#x2F;&#x2F;www.consul.io&#x2F;commands&#x2F;lock</a>
评论 #28413425 未加载
1500100900over 3 years ago
OpenBSD&#x27;s crontab provides a `-s command` flag which does similar thing in principle:<p><a href="https:&#x2F;&#x2F;man.openbsd.org&#x2F;crontab.5" rel="nofollow">https:&#x2F;&#x2F;man.openbsd.org&#x2F;crontab.5</a><p>&quot;-s command<p>Only a single instance of command will be run concurrently. Additional instances of command will not be scheduled until the earlier one completes.&quot;<p>But I don&#x27;t think it saves the state somewhere permanently, which isn&#x27;t a big problem until cron&#x27;s process dies for some reason.
评论 #28412986 未加载
tyingqover 3 years ago
A fairly common pattern would be trying to create a specifically named file with the O_CREAT and O_EXCL flags. That&#x27;s atomic, so you don&#x27;t need flock. If it fails, then exit. This pattern is also often extended where you write your pid to the file and subsequent runs do a sanity check that your pid is actually running, and unlink the file if not.<p>That said systemd-timers, as mentioned in many other comments, is probably the right answer here.
评论 #28414475 未加载
justinatorover 3 years ago
Huh, does python not have a way to flock a file itself?<p>A simple flock doesn&#x27;t always work all that well - the devil is in the details. And there is the issue that you miss a run of the script and have to wait another half hour for the next run.<p>I&#x27;ve used the Highlander, &quot;There can be only one!&quot; code pattern for forever, and it works really well:<p><a href="https:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=14260" rel="nofollow">https:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=14260</a><p>You could change things to check on the status of your lock every minute or so for 15 minutes or whatever.<p>Also in module form,<p><a href="https:&#x2F;&#x2F;metacpan.org&#x2F;pod&#x2F;App::Highlander" rel="nofollow">https:&#x2F;&#x2F;metacpan.org&#x2F;pod&#x2F;App::Highlander</a><p>And much more updated:<p><a href="https:&#x2F;&#x2F;medium.com&#x2F;booking-com-development&#x2F;highlander-b6717278a457" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;booking-com-development&#x2F;highlander-b67172...</a><p>No need to attempt to stuff all this in a cronjob IMHO.
评论 #28412628 未加载
评论 #28412535 未加载
pengaruover 3 years ago
Welcome to Debian&#x27;s dpkg::start-stop-daemon circa 2001<p><a href="https:&#x2F;&#x2F;git.dpkg.org&#x2F;cgit&#x2F;dpkg&#x2F;dpkg.git&#x2F;commit&#x2F;utils&#x2F;start-stop-daemon.c?id=d2c1c54171034f90ed4d3aba80b051f457fd569a" rel="nofollow">https:&#x2F;&#x2F;git.dpkg.org&#x2F;cgit&#x2F;dpkg&#x2F;dpkg.git&#x2F;commit&#x2F;utils&#x2F;start-s...</a>
kbensonover 3 years ago
Or install moreutils from your distro&#x27;s package repo so you can use lckdo and get some other super useful shell helpers too.<p><a href="https:&#x2F;&#x2F;joeyh.name&#x2F;code&#x2F;moreutils&#x2F;" rel="nofollow">https:&#x2F;&#x2F;joeyh.name&#x2F;code&#x2F;moreutils&#x2F;</a>
评论 #28414020 未加载
bumpaover 3 years ago
I have made a simple &quot;cron with extras&quot; project <a href="https:&#x2F;&#x2F;github.com&#x2F;umputun&#x2F;cronn" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;umputun&#x2F;cronn</a> handling duplication issue as well as a bunch of other things, like restarting failed jobs, jitter, automatic reload and so on.
ckdarbyover 3 years ago
How to use systemd timers as a replacement for cron jobs: <a href="https:&#x2F;&#x2F;opensource.com&#x2F;article&#x2F;20&#x2F;7&#x2F;systemd-timers" rel="nofollow">https:&#x2F;&#x2F;opensource.com&#x2F;article&#x2F;20&#x2F;7&#x2F;systemd-timers</a>
aaronrobinsonover 3 years ago
Systemd is much more powerful than cron and manages your exact use case. You will need to deal with process hangs though if you have self dependencies like this.
评论 #28415312 未加载
klysmover 3 years ago
I think systemd timers are vastly superior to cron mostly because of this problem.
评论 #28415722 未加载
pechayover 3 years ago
Add and verify a last_started and last_completed time to your local database.
评论 #28415304 未加载