Saturday, July 18, 2015

cron - Crond offset five minute schedule



Is it possible to offset a cron script set to run every 5 minutes?



I have two scripts, script 1 collects some data from one database and inserts it into another, script 2 pulls out this data and a lot of other data and creates some pretty reports from it. Both scripts need to run every 5 minutes. I want to offset script 2 by one minute so that it can create a report from the new data. E.G. I want script one to run at :00, :05, :10, :15 [...] and script two to run at :01, :06, :11, :16 [...] every hour. The scripts are not dependent on each other, and script 2 must run regardless of whether script one was successful or not. But it would be useful if the reports could have hte latest data. Is this possible with cron?




Post;
I have thought about using both commands in a shell script so they run immediately after each other but this wouldn't work, sometimes script 1 can get hung up on waiting for external APIs etc. so might take up to 15 mins to run, but script 2 must run every 5 minutes so doing it this way would stop/delay the execution of script 2. If I could set this in Cron it would mean script 2 would run regardless of what script 1 was doing


Answer



You can run scripts whenever you want using cron. If you want to run script 1 every 5 minutes, you might start like this:



*/5 * * * * /path/to/script1


But this is really just shorthand for:




0,5,10,15,20,25,30,35,40,45,50,55 * * * * /path/to/script1


If you want to run script 2 one minute after script 1, you can do this:



1,6,11,16,21,26,31,36,41,46,51,56 * * * * /path/to/script2


You could also do this:




*/5 * * * * /path/to/script1
*/5 * * * * /path/to/script2


And then at the start of script 2, sleep for one minute:



sleep 60

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...