I want to know whether there is any easier way to run a job every 25 minutes. In cronjob, if you specify the minute parameter as */25, it'll run only on 25th and 50th minute of every hour
6 Answers
The command in crontab is executed with /bin/sh
so you can use arithmetic expansion to calculate whether the current minute modulo 25 equals zero:
*/5 * * * * [ $(( $(date +\%s) / 60 \% 25 )) -eq 0 ] && your_command
cron
will run this entire entry every 5 minutes, but only if the current minute (in minutes since the epoch) modulo 25 equals zero will it run your_command
.
As others have pointed out, 1 day is not evenly divisible by 25 minutes, so this will not cause your_command
to run at the same time every day, but it will run every 25 minutes.
-
simply genial... – Tomas Aug 10 '11 at 23:12
-
Note that with the cron implementation from the ISC at least you'll need to escape those % characters with backslash. – Stéphane Chazelas May 20 '15 at 06:25
-
@StéphaneChazelas, good point. I went ahead and added the backslashes before percent signs because to my knowledge every implementation of cron requires this. – CODE-REaD Feb 07 '20 at 15:33
Your best bet is to run at 20 minutes or 30 minutes.
You next best might be to trigger every 5 minutes, and then keep an internal count or timestamp, and run every 5th trigger, or if 25 minutes have elapsed since the last run.
More complicated would be to work out the correct times for a day, starting at midnight, and accept the error at the end of the day. This would involve duplicating the crontab entry to the different hours.
More complicated than that would be to work out the times for an entire month, which would involve many copies of the crontab entry to cover the different combinations.
Finally you could implement your own always on daemon, and have that do the scheduling.

- 281
I have no direct experience with it, but fcron appears to do what you want out of the box. According to its documentation you can specify frequencies:
# Get our mails every 30 minutes
@ 30 getmails -all
# make some security tests every 48 hours of system up time,
# force a mail to be sent to root even if there is no output
@mailto(root),forcemail 2d /etc/security/msec/cron-sh/security.sh

- 261
-
-
-
Thanks Caleb. I should've created a tailored example instead of lifting one straight from the documentation. – che2cbs Jul 21 '11 at 16:57
Let me explain in short what's the problem here. Entering 25 in the minute field causes cron
to execute when the current time's minutes equals 25, that is once an hour. You can enter a list of matches, the problem is that 60 (minutes of an hour) isn't divisable by 25, so you need to add several entries based on the hour. The least common multiple of 60 & 25 is 300, that is 5 * 60. so you will need to cycle through 5 hours until returning to your original start. for example:
0, 25, 50
15, 40
5, 30, 55
20, 45
10, 35
...
But here again, 24 (hours a day) isn't divisable by 5, the least common multiple of 5 & 24 is simply 5 * 24 = 120. & so on and on...

- 2,877
-
Maybe the answer should have been a comment on the original question. – Philomath Jul 21 '11 at 18:49
You may play with the last modification time of a file
Create a little script (for example
/usr/local/bin/age
) that will output the age of a file:#bin/sh echo $(( $( date +%s ) - $( stat -c %Z $1 ) ))
Your crontab would look like this (assuming 25 minutes equals 1500 seconds if I'm right)
* * * * * [ $(/usr/local/bin/fileage /var/tmp/your_command.offset) -gt 1500 ] && touch /var/tmp/your_command.offset && your_command
Note that I use /var/tmp
instead of /tmp
because according to the FHS it is not supposed to be deleted on system shutdown/startup.

- 1,610
I think the following should work 0 0/25 * * * ?
-
-
That would execute on the hour once every ... 25 hours? 0/25 ... not even sure what that does but it's not what the OP is after. – Caleb Jul 20 '11 at 22:01