cron job every 5 hours

Fredrik Tolf fredrik at dolda2000.com
Wed Jul 18 03:22:08 UTC 2007


Olivier Nicole <on at cs.ait.ac.th> writes:

>>     Something like:
>> 
>>     minute */5 * * * root path/to/scriptname
>> 
>>     will do the trick.
>> 
>>     Substitute the * in */5 for your desired start time (* being 0).
>> 
>> -Garrett
>> 
>> PS crond won't do 5 hours and every x number of minutes per job (5 hours 
>> + x mins from end to start), just a flat amount of time (5 hours apart 
>> from start to start). If you need that type of 'precision', at will 
>> solve that like Olivier said if you place it at the end of the command.
>
> I am afraid not.
>
> */5 means on every hours that is a multiple of 5, not every five
>  hours. So it will run every day at hour 0, 5, 10, 15 and 20. Between
>  hour 20 one day and hour 0 the next day there is only 4 hours, not
>  the "every 5 hours" requested.
>
> Just to confirm that I launched a cron job yesterday:
>
> 23 */5 * * * /home/java/on/crontest
>
> It ran at 15:23, 20:23 and today at 0:23 and 5:23 and so on:
>
> Date: Wed, 18 Jul 2007 05:23:00 +0700 (ICT)
> From: Olivier Nicole <on at cs.ait.ac.th>
> To: on at banyan.cs.ait.ac.th
> Subject: test crontab 5 hours
> X-Virus-Scanned: on CSIM by amavisd-milter (http://www.amavis.org/)
>
> This is a test for crontab
> [...]
> Only way to run a job every 5 hours is with at(1).

I wouldn't go as far as saying the *only* way. You could make the cron
job run every hour and then have an internal check in it (or using a
wrapper script that checks it). Kind of like this, maybe?

#!/bin/sh
unset nogo
if [ -r /tmp/lastrun ]; then
    now=`date +%H`
    if [ $((($now + 24 - `cat /tmp/lastrun`) % 24)) -lt 5 ]; then
        nogo=y
    fi
fi

if [ "$nogo" = y ]; then exit 0; fi

date +%H >/tmp/lastrun

# Do real work here



More information about the freebsd-questions mailing list