varsha8392 varsha8392 - 2 months ago 7
Linux Question

How to write a cron for every 30 minutes between 2AM to 4AM?

I was trying to figure a cron for every 30 minutes between 2AM to 4AM?
So the cron run time will be: 2:00 2:30 3:00 3:30 4:00

Every hour will be something like this:
0 2,3,4 * * * command

Thank You.

Answer

I'd just write it as two distinct rules:

0,30 2-3 * * * /run/this/command
0    4   * * * /run/this/command

If you're the sort that worries about this sort of thing (I'm not), you can use a conditional to get it onto one line:

0,30 2-4 * * * [[ "$(date +%H%M)" != "0430" ]] && /run/this/command

This will run the command given at 4:30 as well, but not actually call your script unless the time is something other than 4:30.

Comments