How to define every 5 hours in Crontab

Hi Experts,

I want to run a script which will move the files from particular directory to another backup directory After EVERY 5 hour.

How can I put it in crontab-

5 hours!!! * * * * /home/movefilescritp.sh

//purple

Unix Crontab - setting up cron jobs using crontab

  • 0,5,10,15,20 * * * /your/command (not tested!)

Setup the cron to run every hour

At the beginning of the cron, have it do something like the following

time_ct=$(cat /tmp/cron_time_ct)
if [ $time_ct -lt 5 ]
   then
     echo "Not yet time"
     time_ct=$((time_ct+1))
     echo $time_ct > /tmp/cron_time_ct
     exit
   else
     time_ct=0
     echo $time_ct > /tmp/cron_time_ct
fi
# rest of your task

joeyg, isn't it less troublesome to do it as a simple cronjob? why should we have these test cases?

If I understand the request, the goal is to run at the following times (all in 24 hour time):

Sun 05:00
Sun 10:00
Sun 15:00
Sun 20:00
Mon 01:00
Mon 06:00
Mon 11:00
Mon 16:00
Mon 21:00
Tue 02:00
etc..

Thus, there is no regular pattern to supply to cron. And with 168 hours in a week, you cannot even play that since also not divisible by 5.

Could execute the script (with full path name) once in background with "at now", and then use "at now + 5 hours" at the start of the script to spawn the script again. Needs a simple stop flag to stop the process.

#!/bin/ksh
COMMAND_LINE="$0"
if [ -f /tmp/stop_flag ]
then
exit
fi
echo "${COMMAND_LINE}" | at now + 5 hours
# Rest of the script
#

Note: The 5 hours may drift over a period of time due to cron and shell initialisation times.

Won't that actually execute every minute for the hours you specified?
00:00
00:01
00:02
00:03
..
00:59
05:00
05:01
...
etc.

Yes, if you use this example - you can shut down your server :slight_smile:

working example (easy)

1 0,5,10,15,20 * * * user /your/command

another example (simple)

1 */5 * * * user /your/command

your command will be run in:
0.01
5.01
10.01
15.01
20.01

and this one won't work in solaris (afaik).