How can I make sure a scheduled task does not execute a new script run in case a previous one is still busy?
I have a scheduled task that runs every X minutes.
In rare / exceptional cases it is possible that the execution of the script takes longer than this time.
In those cases I rather have the next scheduled task trigger to skip the execution and wait until the running script has finished.
This to prevent an increasing load / queue of scripts running.
I also don't want to decrease the frequency of the scheduled task as in normal situations it would then not update fast enough.
I just have to cover the exceptional case.
Hi Mieke,
I don't know if there is a better way to handle this, but one approach could be to introduce an extra layer between the scheduled task and the actual script.
The scheduled task would still trigger every X minutes, but instead of directly starting the actual script, it would trigger a small wrapper/guard script.
That wrapper script would first check if the actual script is already running. If it is, it simply does nothing and exits. If it isn't, it starts the actual script.
I think it makes sense to have this check in a separate wrapper script rather than in the actual script itself. Once the actual script has started, it is already too late to determine whether another instance should have been started by the scheduler. The wrapper can make that decision before triggering the actual script.
This way, you can keep the current scheduling frequency for normal situations, while preventing multiple instances of the script from running at the same time when an execution takes longer than expected.
Something along the lines of:
Scheduled task → wrapper script → actual script
In order to determine if a particular script is already running, you can follow the approach described here:
Verify if automation script is (still) running - DataMiner Dojo
PS: If you find a way to retrieve the number of active runs of a particular script, then you wouldn't need the wrapper script. You could simply check the number of active runs before starting a new one. If the count is one, you know there is no other instance running.
Kind regards,