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
The minute entry field for crontab accepts an "increments of" operator that is kind of confusing because it looks like it should be a mathematical "divide by" operator but isn't. You will most often see it used something like the following. Note that this does not find numbers that are divisible by five but rather takes every fifth item from a set:
This tells cron to match every fifth item (
/5
) from the set of minutes 0-59 (*
) but you can change the set like this:This would take every fifth item from the set 1-59, running your command at minutes 6, 11, 16, etc.
If you need more fine grained offsets than one minute, you can hack it using the sleep command as part of your crontab like this:
This would run your job every five minutes, but the command would not actually start until 15 seconds after the minute. For short running jobs where being a few seconds after something else makes all the difference but you don't want to be a full minute late, this is a simple enough hack.
You can run scripts whenever you want using
cron
. If you want to run script 1 every 5 minutes, you might start like this:But this is really just shorthand for:
If you want to run script 2 one minute after script 1, you can do this:
You could also do this:
And then at the start of script 2, sleep for one minute:
You can indicate a time offset with the + symbol. For example, to run at
:01, :06, :11, :16 [...]
, create a task such asThis worked for me:
Where 1 is the offset minutes. So if you want to offset three minutes:
I have this working in AWS schedule settings
This is an XY problem. Offsetting cron from whole hour intervals, or common fractions thereof, can be generally valuable to reduce coinciding processes, but this question explicitly states the offset is to resolve a dependency. Script 2 relies on script 1, so using time alone to enforce a sequence creates a delay at best and a race condition at worst.
It's better to put them into the same cron job line. If the second command should run regardless of the first script's outcome (or the first doesn't use correct, non-zero exit codes on failure), replace the
&&
with;