Link to home
Start Free TrialLog in
Avatar of Gary Antonellis
Gary AntonellisFlag for United States of America

asked on

CRON statement to run every 3 minutes except on Monday morning 12am to 2am

We need to run a service every 3 minutes except while Payroll is being processed which is Monday morning 12am to 2AM
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Put this into the user crontab

*/3 * * * * [[ "$(date +\%a)" == "Mon" && $(date +\%H\%M) -ge 0000 && $(date +\%H\%M) -le 0200 ]] || /path/to/service_script

Open in new window

Please note that in crontab the percent signs must be escaped with a backslash ("\%")

In the system crontab add the required username as the sixth field ("*/3 * * * * username [[ ... ...")
Instead of messing with a very complicated cron, you can do this:

if [ $(date +%u) = 1 ]                                       
 then 
      if [ $(date +%H) = 00 -o 01  ] 
      then
    
       echo do nothing
       fi

else
   echo my command to execute goes here
exit
fi

Open in new window


First condition checks to see if it's Monday (1), next condition checks to see if it's between hour 00 or 01 (literally 12AM to 1:59AM), and does nothing but echos "do nothing"

Place the actual script / job cron is running every 3 min where you see "echo my command to execute goes here".

Then, you can use a very simple every 3 min in your cron.
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@omarfarid,

you could turn your really fine solution into a perfect one by replacing "0,1" with "2-23", don't you think so?

The author requires the task not to run between 0:00 and 1:59!
Thank you for the correction :)
Avatar of Gary Antonellis

ASKER

I am getting a error that both expressions are invalid in CRONMAKER.com.  I am also getting an error when I try saving the Cron string in our application, which uses Java quartz.  Is this a Cron syntax not supported by Java?

Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial