0

I'd like to use Python to run some Windows .bat files which would call some commercial .exe software.

However, the commercial software will likely take a long time to finish or get stuck. If the software completes, it would quit itself automatically. Otherwise it'll stay open indefinitely. This may be used as an indicator of completeness for simplicity (although not 100% guarantee) as I don't know how to get some success return code status if the software successfully completes its job.

I want the Python script to retry running the commercial software if it doesn't finish after say 30 mins. The software needs to be terminated before retrying, if it is reopened before being terminated, there would be error.

My attempt:

Use Python threading to create a thread for each running of a .bat calling the commercial software. The starting of the bat is done by os.system('<pathname>\<batfilename> <arguments needed>') The mother thread would measure the time elapsed by the child thread and if it exceeds 30 mins, then the mother thread would terminate the thread and start it again. However, I'm not sure if the termination of the thread would cause the software to close. I also do not know how to measure the time taken while the thread is still running as I see most codes online are about joining which needs the child thread to finish and they are measuring the finish_time - start_time. But here the software would still be running when stuck

AlgoManiac
  • 99
  • 6
  • 1
    Is there a need to have the batch file in between? You run the risk that launching the batch file will launch the application in a way that won't terminate it when you terminate the batch file after the timer expires. Please share some code that shows how you launch the thread, how you launch the batch file and how the batch file starts the actual application. – Grismar Jul 25 '22 at 03:33
  • 1
    Call the `exe` from python in process with a time loop, terminate the process when your timer runs out. – monkut Jul 25 '22 at 03:36
  • @Grismar I think theoretically the batch isn't necessary. I will try to see if it'll work without it – AlgoManiac Jul 25 '22 at 03:46

1 Answers1

1

Share your effort in code. Here's a sample implementation.

import datetime
from subprocess import Popen

cmd = ("mycommand.exe", "-o", "option1")
p = Popen(cmd, shell=True)

timeout_seconds = 60
start = datetime.datetime.now()
while True:
    now = datetime.datetime.now()
    elapsed = now - start
    return_code = p.poll()  # returns None if not finished
    if return_code or elapsed.total_seconds() > timeout_seconds:
        break
p.terminate()
monkut
  • 42,176
  • 24
  • 124
  • 155
  • Thanks for your reply! I haven't managed to implement a working code for it. I'll try this framework out to see if it works! Also, I want to ask why use multiprocessing instead of multithreading? Is it possible to achieve the same outcome with multithreading? – AlgoManiac Jul 25 '22 at 03:48
  • sorry should have used `subprocess.Popen()` instead of `multiprocessing.Process`. Using Popen over threading gives you more control over the invoked process, and you can terminate it via the returned process object. – monkut Jul 25 '22 at 04:06
  • I tried using Popen, but it gives me the error of 'The working directory does not have write access permission' whereas if I run with os.system(bat file) then using the batfile to call the exe it works fine. – AlgoManiac Jul 25 '22 at 05:28
  • Tried calling the software directly with os.system and it still works. No idea why subprocess Popen gives me this error. Also tried adding 'runas' ... before the software in the cmd string list but it just gives me the "RUNAS USAGE" in the terminal... – AlgoManiac Jul 25 '22 at 06:14
  • This may help: https://stackoverflow.com/a/4813571/24718 – monkut Jul 25 '22 at 07:08
  • I'm not sure why but when I run in the terminal with a string of the whole command with the arguments i.e. 'C:/Program (x86)/ABC Scripts/abc.exe arg1 arg2...' it works fine (without the single quotes). But when I do Popen(the string), it does not work. It says something like C:/Program not a command, so I tried quoting the entire path of the exe i.e. '"C:/Program (x86)...abc.exe arg1 arg2' but it still doesn't work. Then I tried quoting the directories along the path with space, i.e. 'C:/"Program (x86)"/...' but it gives another sort of error. Why it works fine when I type manually on cmd? – AlgoManiac Jul 25 '22 at 13:37