samedi 10 septembre 2022

How can i run a batch script a random amount of times over a specified period each day, with a random amount of time between each run?

I have a batch script, which i initiate every day at 7am and i want this to run no longer than (approximately) 12 hours. During this 12 hour period, i want this batch script to run a python file a random amount of times (between 4 and 8). So far, i have this batch script to divide the 12 hour period by the random amount of times and run the python file with an equal time delay between each run.

This is working nicely, however, i would like to try and take this a step further, whereby the Python file is run with a random time delay (subject to a minimum) between each run and the total delay during the day equals 12 hours.

I am not sure how to do this, but have an idea to start. I was thinking of having a delay 'pot' of 12hrs * 60mins = 720mins. First calculating a random number, say X, between 4 and 8. Then I need to calculate X amounts of random time delays that all add up to 720 and each delay is a minimum of say 30. Any help or pointers would be appreciated.

So far, my batch script looks like this:

REM Generate random integer between 4 and 8
set /a rand_n=%random% %%5 + 3
echo %rand_n%

REM Calculate interval
set /a interval=(12*60*60)/(%rand_n%)
echo %interval%


REM for /l %%x in range(%rand_n%) do (
for /l %%i in (1,1,%rand_n%) do (

    REM Delay
    timeout %interval%

    REM Run Python script
    py python_file.py
    
    )



Aucun commentaire:

Enregistrer un commentaire