We help IT Professionals succeed at work.

Kill process if it is older than 30 seconds

rustyrpage asked
We have a shipping application that our ERP vendor has written that has a bug in it when it interfaces with a third party USPS module.  Basically, the ERP system passes off an XML file to the third party USPS application, which runs, prints the label & then closes out.  The problem is that if there is any error, it will just lock up the whole application & someone with server access has to go in & kill the EXE.  So, I have two options to fix this:

1) Make a scrip that checks to see if dazzle.exe has been running for more than 30 seconds & if so, kill it.  (how intensive would this be from a resource standpoint?)

2) Make a shortcut that our shipping folks can click on that would kill dazzle.exe on the server (keeping in mind that they obviously are not admins on the server, so I would have to determine a secure way to get around that)

Any help would be greatly appreciated.
Watch Question

The first one is relatively easy with powershell. Is powershell solution acceptable for you?


What are the cons/downsides?  How much of a performance hit is there?

Powershell takes some resources and has to be installed on 2003.
You an use simple script checking start time for your process and killing it. This script has to be launched periodically.
How would you like to launch your solution? Maybe Task Scheduler is an option? If so - entire solution has acceptable performance impact.
Most Valuable Expert 2018
Distinguished Expert 2018
I'd suggest a "wrapper" that starts the program, monitors it, and kills it if it doesn't finish in time.
Below is a script for AutoIt; save it as TimeoutRun.au3, go to http://www.autoitscript.com and download AutoIt and the full SciTe editor, and compile the script to an exe file (as console application).
The "script" can be configured with an ini file named "TimeoutRun.ini" in its folder (see below).
It will start the program defined in the ini file, and pass it all arguments you passed to the script, and then terminate the program if it doesn't stop by itself.
You can rename dazzle.exe to dazzle-originial.exe and the script to dazzle.exe, so that you don't have to change any external code calling this program.

; The path and name of the program to run (do not use quotes);
; all arguments passed to the TimeoutRun will automatically be passed to the program.
Run = C:\WinNT\system32\notepad.exe

; maximum time the process is allowed to run in seconds:
Timeout = 30

; time between process queries in milliseconds:
Polltime = 500

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Skript for AutoIT (http://www.autoitscript.com/)
Starts the program defined in the ini file, and kills the process after the timeout specified if it's still running
Any command line arguments will be passed unchanged to the program started
Posted at Experts Exchange by oBdA (http://www.experts-exchange.com/M_960827.html)
0 - External program started correctly and finished before timeout
1 - External program started correctly and killed after timeout
2 - Internal error: ini file not fund
3 - Internal error: program defined in the ini file not fund
Tested with AutoIT v3.3.6.1; use Aut2exe.exe to compile this script into an executable.
AutoItSetOption("TrayIconHide", 1)

Global Const $strIniFile = @ScriptDir & "\TimeoutRun.ini"

Func _Main()
Local $intPid
Local $strRunProgram
Local $intRuntime, $intTimeout
	If Not FileExists($strIniFile) Then
		ConsoleWrite("Error, Inifile not found: " & @CRLF & $strIniFile & @CRLF)
		$strRunProgram =	IniRead($strIniFile, "TimeoutRun", "Run", "")
		$intTimeout =		IniRead($strIniFile, "TimeoutRun", "Timeout", 60)
		$intPolltime =		IniRead($strIniFile, "TimeoutRun", "Polltime", 500)
		$strArguments = 	$CmdLineRaw
;		When doing test runs out of SciTE, set $strArguments to the program you want to start; $CmdLineRaw will contain arguments
;		from the wrapper in addition to the arguments specified in the View menu.
;		$strArguments = "C:\Temp\test.txt"
		$intRuntime = 0
		$intTimeout *= 1000
		$intPid = Run('"' & $strRunProgram & '"' & " " & $strArguments)
		If ($intPid = 0) Then
			ConsoleWrite("Error, program not found: " & @CRLF & $strRunProgram & @CRLF)
			While ($intRuntime < $intTimeout)
				If ProcessExists($intPid) Then
					$intRuntime += $intPolltime
					ConsoleWrite($strRunProgram & " finished after " & $intRunTime & "ms." & @CRLF)
			ConsoleWrite($strRunProgram & " was killed after " & $intTimeout/1000 & " seconds" & @CRLF)

; ****************************************************************************************************
; ****************************************************************************************************

Open in new window