0

I want to start a python script and then automatically close that script after 2 minutes, run another command, and keep doing the same thing again like this (loop) forever :

Cd c:/location.of.script/
pythonscript.py
Stop (like ctrl+c) pythonscript.py after 120s
Del -f cookies.file
.
.
. 

Is this even possible with a batch file on windows 10? If so, can someone please help me with this?

I’ve been looking everywhere but found nothing except the exit() command which stops the script from inside - this isn’t what I want to do.

cactus4
  • 128
  • 9
  • 1
    https://stackoverflow.com/questions/2888851/how-to-stop-process-from-bat-file + https://stackoverflow.com/questions/1672338/how-to-sleep-for-five-seconds-in-a-batch-file-cmd + https://stackoverflow.com/questions/893203/bat-files-nonblocking-run-launch are the tools you need. – Karl Knechtel Oct 19 '19 at 03:04
  • Easier to do with PowerShell? Also might be a better question for https://superuser.com/ – Tim Oct 19 '19 at 03:08
  • @tim how can I make it easier with powershell ? – Badr Wahbi Oct 19 '19 at 12:34

1 Answers1

0

You can change your python script to exit after 2 minutes, and you could batch file that has a while loop that runs forever and run the python script then deletes the cookie.file, I don't know if that's exactly what you want, but you can do it by putting a timer in your python script.

You can make a separate thread that keeps track of the time and terminates the code after some time.

An example of such a code could be:

import threading

def eternity(): # your method goes here
    while True:
        pass

t=threading.Thread(target=eternity) # create a thread running your function
t.start()                           # let it run using start (not run!)
t.join(3)                           # join it, with your timeout in seconds

And this code is copied from https://stackoverflow.com/a/30186772/4561068

Waqar Bin Kalim
  • 321
  • 1
  • 7
  • The code have to be put inside the python script right ? So it need to be on the start of that script right ? Because I suck at python and I don’t really want to touch that code I prefer if there is a way to do it from batch file or powershell as another user suggested. – Badr Wahbi Oct 19 '19 at 12:39