Tea
Tea

Reputation: 11

How do I run 100 python files 2 minutes apart?

I want to run around 100 python scripts 2 minutes apart everyday to ensure that none of them overlap. I am using linux/Mac system.

Is there a dynamic way of doing this using cron tab? Or perhaps is there a scheduler program that might make it easier?

Upvotes: 1

Views: 265

Answers (3)

Kent Shikama
Kent Shikama

Reputation: 4060

Edit: This does not answer the OPs question but I will leave it up here in case anyone else misinterpreted the question title and came searching for the corresponding answer.

I would write a (most likely bash) script that will execute all 100 scripts and then call that one script with crontab. The cron line for every 2 minutes is as follows:

*/2 * * * * <file>

Here is a bash script that runs all python scripts in a directory, assuming all 100 scripts are in the same directory (taken from here).

for f in *.py; do python "$f"; done

Upvotes: 1

Sam Gaudet
Sam Gaudet

Reputation: 61

Is the goal here to have the files execute exactly two minutes apart, or are you hoping to have one job just finish before the other executes?

If you are solely concerned with script1 finishing before script2, crontab should be able to do this in cronjob with a command like this:

script1 && script2

There is a decent example of this in the comments of this Reddit post.

If you do in fact want to have the scripts execute exactly 2 minutes apart, maybe you could approach by setting a specific execution time? Of course this may be somewhat sensitive to failures / not the most robust method, so adding some sort of event listener, etc. might be a better option.

Hope this helps a little bit!

Upvotes: 2

BillyN
BillyN

Reputation: 185

I think the easiest way would be to develop your own python script to manage this, using the python time module to add the delay.

import time
time.sleep(120) # 2 minute delay. 

To call your script just import the file then execute it by running.

exec('file.py') #this is for python 3 

Upvotes: 2

Related Questions