0

I've searched for this but cant seem to find an answer. I was reading the following post Cron Job for Every 3 days which is almost what i need:

The command for “day of the year” is

date +%-j

Where the hyphen before the “j” means “don’t pad” (with zeros in this case – so we can use it as a number). Now we just check if this is evenly divisible by three by using the modulo – % – which returns the remainder of dividing.

$(( 'date +%-j' % 3 ))

The $(( )) part tells bash to perform a calculation, and the backticks are there to run our date command separately from the rest. So if this calculation is zero, we have a winner.

[ $(( 'date +%-j' % 3 )) == 0 ]

Remember, though, we need an offset so that we start on the 1st. The way it is currently, it starts on the 3rd. Just by subtracting one from the day of the year, we can get everything to line up. We have to wrap that calculation with $(( )) as well.

[ $(( $(( 'date +%-j' - 1 )) % 3 )) == 0 ]

That test fulfills our conditions so our full crontab entry would be:

30 05 * * * [ $(( $(( 'date +%-j' - 1 )) % 3 )) == 0 ] && execute_this

However i have a specific case to run this every 2.5 days. Is this not at all possible in cron? Is it a matter of switching the 3 our for 2.5 or not?

i know i can do 2 and a half hours by specifying minutes instead, but 2.5 days?

For information I'm using the Whenever Gem in a Ruby on Rails application, but may have to edit cron manually.

Any advice here is greatly appreciated.

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
Cliff
  • 31
  • 4
  • Possible duplicate of [How to run crontab for every 36 hours](http://stackoverflow.com/questions/17726620/how-to-run-crontab-for-every-36-hours) – Eyeslandic Feb 02 '17 at 10:54
  • i'm going to try the solution by johnnyfaldo from that post then. however 2 questions related to it. 1. what would happen if the server went down for a period and 2. what would happen at the end of the month? would the counter carry on as expected. – Cliff Feb 02 '17 at 11:01
  • Well, it's not my speciality, but I wouldn't count on a counter in this instance. This here sounds like a more sophisticated solution perhaps http://askubuntu.com/questions/646684/how-to-run-a-cron-job-every-36-hours – Eyeslandic Feb 02 '17 at 11:05
  • ok thanks for that. i'll take a look. i have an instance that i want to run every 89 hours. woe is me – Cliff Feb 02 '17 at 11:09
  • Those are some curious requirements, all parts of the joys of programming I guess. – Eyeslandic Feb 02 '17 at 11:29

0 Answers0