1

I'm running a kind of touchy .exe file in Python to receive a couple of data measurements. The file should open, take the measurement, then close. The issue is sometimes it crashes and I need to be able to take these measurements every 10 minutes over a long period of time.

What I need is a 'check' to see if the .exe is not responding and if it's not, then to have it kill the process. Or to just kill the whole script after every measurement taken. The issue is that the script gets stuck when it tries to run the .exe file that's not responding.

Here's the script:

FNULL = open(os.devnull, 'a')   

filename = "current_pressure.log"

command = '"*SRH#\r"'

args = "httpget -r -o " + filename  + " -C 2 -S " + command + IP 

subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)

Basically, need something like:

"if httpget.exe not responding, then kill process" 

OR

"kill above script if running after longer than 20 seconds"
1
  • I would read httpget docs to find out whether it has deadline, timeout options. You could use timeout parameter with subprocess.call() on Python 3. Commented Apr 3, 2015 at 19:33

2 Answers 2

1

Use a timer to kill the process if its gone on too long. Here I've got two timers for a graceful and hard termination but you can just do the kill if you want.

import threading

FNULL = open(os.devnull, 'a')   
filename = "current_pressure.log"
command = '"*SRH#\r"'
args = "httpget -r -o " + filename  + " -C 2 -S " + command + IP 

proc = subprocess.Popen(args, stdout=FNULL, stderr=FNULL, shell=False)

nice = threading.Timer(20, proc.terminate)
nice.start()
mean = threading.Timer(22, proc.kill)
mean.start()
proc.wait()
nice.cancel()
mean.cancel()
Sign up to request clarification or add additional context in comments.

5 Comments

Awesome! That seems to be working. I'll let you know if I get any more crashes. Thank you so much!
It still crashes, I get a popup window that says "httpget.exe has stopped working", but now the program is able to keep running even with this open, so I guess the problem is solved. I don't mind the popups windows as long as it keeps running. Thanks again!
On Windows, kill() is an alias for terminate(). args is a string + shell=False means Windows here.
So I need to be able to leave the program running and taking measurements every 10 minutes. When it crashes, I get a popup window saying something like "httpget.exe is not responding", all I need to do to continue the program is press 'enter' or click the option in the popup window, then the program continues normally. Is there a way to make it so that 'httpget.exe' automatically just accepts that click option in the popup window when it's not responding? Or to quit the exe when it's not responding. Something that exists outside of my program, within the windows control panel or something?
@NathanMiller - that's a more difficult problem. Apparently its a GUI application so you are dependent on whether its developer gives you a scriptable way to run it (e.g., command line arguments to exit on error instead of a popup). There are a lot of GUI automation toolkits you could use to "drive" the application, but I don't use them and don't have a good suggestion about which ones are good.
1

Generally, when a program hangs while working on windows we try to go to Task Manager and end the process of that particular program. When this approach fails, we experiment with some third party softwares to terminate it. However, there is even another better way for terminating such hanged programs automatically

http://www.problogbooster.com/2010/01/automatically-kills-non-responding.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.