24 points · by the2ndfloorguy · 5y ago · 45 comments
How, exactly ?
I've been a cron user forever, I had never heard of systemd timers until this thread.
A quick internet search shows:
- Nice clean config in a file (instead of having to edit crontab)
- Lots of great features that you would have to manually implement and maintain in cron (e.g. missed launches, duplicate runs, random delay, timeouts)
I mean sure, I've got bash script templates that let me solve the problem of preventing duplicate cron job running.But having seen how easy it is to achieve the same thing with systemd timers whilst not requiring a whole lot of boilerplate code in each shell file ... why wouldn't you switch to systemd timers ? I sure will be.
I like systemd. It's a good clone of launchd
https://man.openbsd.org/crontab.5
"-s command
Only a single instance of command will be run concurrently. Additional instances of command will not be scheduled until the earlier one completes."
But I don't think it saves the state somewhere permanently, which isn't a big problem until cron's process dies for some reason.
The approach you're suggesting is what led to the problem you're proposing it as a solution for.
That said systemd-timers, as mentioned in many other comments, is probably the right answer here.
The problem with creating files is that you then have the stale lock file problem to deal with which adds a lot more complication.
There's also sysv or posix semaphores.
A simple flock doesn'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.
I've used the Highlander, "There can be only one!" code pattern for forever, and it works really well:
https://www.perlmonks.org/?node_id=14260
You could change things to check on the status of your lock every minute or so for 15 minutes or whatever.
Also in module form,
https://metacpan.org/pod/App::Highlander
And much more updated:
https://medium.com/booking-com-development/highlander-b67172...
No need to attempt to stuff all this in a cronjob IMHO.
It does. (https://docs.python.org/3/library/fcntl.html#fcntl.flock)
glances around nervously is flocking one of those "what's old is new again" concept that people are just now relearning? Surely a blog post on how not to have two concurrent cronjobs running is kind of a weird thing to read when some Linux grey beard figured this out in the 90's on their lunch break.
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/utils/start-s...
If you're writing the program, it's fairly easy to do yourself in it with Flock or Fcntl (you can usually just get an exclusive lock in the absolute path to the running executable/script.
That said, quite a few of those moreutils items are useful in their own right (e.g. ts to prefix timestamps to each line of output is something so simple but so useful for logging output, chronic to silence stdout unless exit error is also very useful for cron, etc).