News for hackers. Stuff that matters.
Re:Cron best practices by dschuck on Friday March 11, 2022 @08:12AM (#30639380)
0 23 star/2 star star insert_your_script_here.sh

i.e. runs every two days at 11pm. or am i mistaken?

Re:Cron best practices by tjader on Friday March 11, 2022 @08:37AM (#30639687)
That will run on even days. That means that when a month with 31 or 29 days ends it won't run for two days in a row, as the day will go 30, 31, 1, 2.
Re:Cron best practices by dinom on Friday March 11, 2022 @08:37AM (#30639688)
The problem is some months have an odd number of days thereby causing the job to run two days in a row.

I'd consider offloading the work to the shell if you want to run things every other day, e.g.:

* * * * * [ $(($(date +\%w) \% 2)) -eq 0 ] || echo skipped-a-day

Re:Cron best practices by dschuck on Friday March 11, 2022 @09:41AM (#30640460)
Dang, true. Didn't catch that. I think sometime I had set cron to run a script every day at a certain time and bypassed every other day with a simple .txt file.

Top of my script was a simple if-clause, when alternate-day.txt is found, it will be removed and the script runs. If no .txt is found it will be created and the script exits. A bit lame, but it worked and was quick :)

Re:Cron best practices by dinom on Friday March 11, 2022 @11:37AM (#30642096)
Interestingly, and on further consideration, using date with %w (as in the example I provided) suffers from the same problem, lol! Creating a file as you did should work nicely. Or a fair bit more arithmetic could be done using date's %s, not my cup of tea though.

As mentioned, the next problem appears when the job takes longer than two days, which requires using flock. I often prefer to use mkdir instead of flock, provided your mkdir is atomic and POSIX compliant... which is usually the case AFAIK. E.G. mkdir a-lock-dir || exit;... rmdir a-lock-dir

Ultimately I don't have too many complaints about cron. But, like anything, it has it's own quirks. Indeed a handy article.

Re:Cron best practices by soneil on Friday March 11, 2022 @10:04PM (#30648623)
yeah, calendar manipulation is trickier than it looks. If you use day-of-week, you have an odd number of days. If you use day of month, many months have an odd number. If you use day of year (%j), most years have an odd number.

There's many such examples - every 1, 2, 3, 4, 6 hours works - but every 5 hours doesn't. But every 2 days and every 14 days are requirements I've met IRL.