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
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
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.
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.
# 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
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
5, 30, 55
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...
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.