Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

migrated from stackoverflow.com Jul 20 '11 at 16:10

This question came from our site for professional and enthusiast programmers.

6 Answers

up vote 12 down vote accepted

The command in crontab is executed with /bin/sh so you can use arithmetic expansion to calculate whether the current minute modulo 25 is zero.

*/5 * * * * [ $(( $(date +%s) / 60 % 25 )) -eq 0 ] && your_command

cron will run the whole command every 5 minutes, but only if the current minute (in minutes since the epoch) modulo 25 is 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.

share|improve this answer
    
simply genial... –  Tomas Aug 10 '11 at 23:12
add comment

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.

share|improve this answer
add comment

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...

share|improve this answer
    
Maybe the answer should have been a comment on the original question. –  Philomath Jul 21 '11 at 18:49
add comment

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
share|improve this answer
    
Where do you see 25 minutes here? –  Philomath Jul 21 '11 at 10:56
    
Using fcron and @ 25 command should do the trick. –  Caleb Jul 21 '11 at 12:40
    
Thanks Caleb. I should've created a tailored example instead of lifting one straight from the documentation. –  che2cbs Jul 21 '11 at 16:57
add comment

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.

share|improve this answer
add comment

I think the following should work 0 0/25 * * * ?

share|improve this answer
    
As per my knowledge it'll work for every minute. –  Pappu Jul 20 '11 at 12:30
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.