Running/Killing an executable after 20 minutes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
GilesTRAbbit
Posts: 4
Joined: 10 Dec 2015 15:07

Running/Killing an executable after 20 minutes

#1 Post by GilesTRAbbit » 10 Dec 2015 15:22

Hi,
Batch file scripting is brand new to me, so please be patient with me...

In order to run calculations in a program called GULP on a windows (10, 64-bit) environment, I use the .bat file that has the following form:

Code: Select all

gulp.exe < input-1.dat > output-1.out
gulp.exe < input-2.dat > output-2.out
gulp.exe < input-3.dat > output-3.out
                 .
                 .
                 .
gulp.exe < input-5076.dat > output-5076.out

This .bat file is found in the same folder as both the GULP executable (gulp.exe) the .dat input files [simple text files] being passed into the executable.

These calculations should be relatively quick in running (minutes at the most). Many will terminate "because of an error". Unfortunately however, some of the calculations will hang (for unknown reasons)...currently, when this occurs, I manually kill the whole batch command...which, means I have to keep an eye on how the calculations are progressing and cannot simply leave them running (e.g. overnight)

Thus, I am looking for a way of automatically killing the gulp.exe executable if it has been running for 20mins, in a way that enables the next calculation in the list still runs.
i.e. if calculation 2 hangs, kill it and run calculation 3
Ideally, the number of the killed calculation - obtained from the input/output filenames - would be written to a new text file.

Any pointers as to how this may be achieved would be very gratefully received :-)

Regards,
GilesTRAbbit

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running/Killing an executable after 20 minutes

#2 Post by foxidrive » 10 Dec 2015 15:48

This might do the job, and replace your batch file: (untested)

It launches each command and waits for 10 seconds
- if the process is still running after 10 seconds then it waits for 20 minutes and checks if gulp is still running - and kills it if it is with an entry in file.log

So the 10 seconds should be changed to the kind of length the process usually takes - so a normal run will work as intended and a run that takes longer will be given 20 minutes (1200 seconds) to complete.



Code: Select all

@echo off
for /L %%a in (1,1,5076) do (
      start "" gulp.exe <input-%%a.dat >output-%%a.out
   set "ok="
   timeout /t 10 /nobreak
   tasklist |find /i "gulp.exe" >nul || (set ok=1)
   echo file %%a
   if not defined ok timeout /t 1200 /nobreak
   if not defined ok tasklist |find /i "gulp.exe" >nul && (echo file %%a failed >> file.log & taskkill /f /im gulp.exe)
)



This is a variation on the theme:

The script will test to see if gulp is still running after 30 seconds, and 90 seconds and 210 seconds
and if it is finished after any of those tests then the next file will be launched.

Only if it is still running after the third test it will be given the full 20 minutes before checking again.

Code: Select all

@echo off
for /L %%a in (1,1,5076) do (
      start "" gulp.exe <input-%%a.dat >output-%%a.out
      set "ok="
   echo test 1 input-%%a.dat
   timeout /t 30 /nobreak
   tasklist |find /i "gulp.exe" >nul || (set ok=1)
      if not defined ok echo test 2 input-%%a.dat
      if not defined ok timeout /t 60 /nobreak
      if not defined ok tasklist |find /i "gulp.exe" >nul || (set ok=1)
         if not defined ok echo test 3 input-%%a.dat
         if not defined ok timeout /t 120 /nobreak
         if not defined ok tasklist |find /i "gulp.exe" >nul || (set ok=1)
            if not defined ok echo long run input-%%a.dat
            if not defined ok timeout /t 1200 /nobreak
            if not defined ok tasklist |find /i "gulp.exe" >nul && (echo input-%%a.dat failed >> file.log & taskkill /f /im gulp.exe)
)

GilesTRAbbit
Posts: 4
Joined: 10 Dec 2015 15:07

Re: Running/Killing an executable after 20 minutes

#3 Post by GilesTRAbbit » 10 Dec 2015 19:36

Hi Foxidrive,

Many thanks for your response.

I have tried the first of the codes you posted - unfortunately with no success: the GULP executable runs - a new window opens and the GULP header can be seen. [I note that this does not usually happen when I use the batch file I described...] But, that is all that happens. It appears to me that the GULP executable is awaiting some information - information that is contained within the .dat input file which presumably isn't being read...

Have you got any thoughts on what may be going wrong?
(Sorry, as I said in my original post, batch scripting is new to me so I know little or no syntax)

I am sure that, once the data in the input files an be read, the code will do exactly want I wanted otherwise...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running/Killing an executable after 20 minutes

#4 Post by foxidrive » 12 Dec 2015 03:59

GilesTRAbbit wrote:I have tried the first of the codes you posted - unfortunately with no success: the GULP executable runs - a new window opens and the GULP header can be seen. [I note that this does not usually happen when I use the batch file I described...]


Are your filenames actually input-1.dat where the numeral is the only changing item?

So if you run this, does it work correctly or hang?

Code: Select all

gulp.exe <input-1.dat >output-1.out

GilesTRAbbit
Posts: 4
Joined: 10 Dec 2015 15:07

Re: Running/Killing an executable after 20 minutes

#5 Post by GilesTRAbbit » 12 Dec 2015 10:50

foxidrive wrote:Are your filenames actually input-1.dat where the numeral is the only changing item?

My filenames are actually "Ti4+potentialfitting-brannerite-1.dat" with the number (which increases by 1) the only thing that changes.

foxidrive wrote:So if you run this, does it work correctly or hang?

Code: Select all

gulp.exe <input-1.dat >output-1.out

Yes the calculations run correctly - with no new window being opened - when I use the code:

Code: Select all

gulp.exe <Ti4+potentialfitting-brannerite-1.dat > Ti4+potentialfitting-brannerite-1.out

Just a though, could it be the '+' and '-' in the filenames that are causing the problem with the script?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Running/Killing an executable after 20 minutes

#6 Post by Squashman » 12 Dec 2015 13:53

GilesTRAbbit wrote:Just a though, could it be the '+' and '-' in the filenames that are causing the problem with the script?

No. You said your file names were named this.
input-1.dat
input-2.dat

So you should be able to see from the code you were given that is what is being used as input.

Code: Select all

start "" gulp.exe <input-%%a.dat >output-%%a.out

The %%a variable just counts up from 1 to 5076.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Running/Killing an executable after 20 minutes

#7 Post by penpen » 12 Dec 2015 14:49

It could be (i'm unsure), when using 'start "" gulp.exe <input-%%a.dat >output-%%a.out' you instanciate a new command shell, with own stdin and stdout (no matter how you initialized "start-stdin/-out"; this (tested with batch file) may fix it:

Code: Select all

start "" cmd /C ^""gulp.exe"  ^<"input-%%a.dat" ^>"output-%%a.out" ^& exit^"

penpen

Edit: Removed a flaw: Missing last escaped doublequote.
Last edited by penpen on 13 Dec 2015 19:32, edited 1 time in total.

GilesTRAbbit
Posts: 4
Joined: 10 Dec 2015 15:07

Re: Running/Killing an executable after 20 minutes

#8 Post by GilesTRAbbit » 13 Dec 2015 15:18

penpen wrote:It could be (i'm unsure), when using 'start "" gulp.exe <input-%%a.dat >output-%%a.out' you instanciate a new command shell, with own stdin and stdout (no matter how you initialized "start-stdin/-out"; this (currently untested) may fix it:

Code: Select all

start "" cmd /C ^""gulp.exe"  ^<"input-%%a.dat" ^>"output-%%a.out" ^& exit

penpen


afraid not. nothing happens (no new windows open; no new/output files are created and GULP doesn't appear to run) when the appropriate line is changed to the this...

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Running/Killing an executable after 20 minutes

#9 Post by penpen » 13 Dec 2015 19:47

Sorry, i've missed the closing (escaped) doublequotes in my above post; Now it should work, Tested with this batch:

Code: Select all

:: test.bat
@echo off
setlocal
set /P "input="
echo input=%input%
endlocal

Code: Select all

Z:\>start "" cmd /C ^""test.bat"  ^<"test.bat" ^>"testOut.txt" ^& exit^"

C:\Users\Ulf\Desktop>type testOut.txt
input=:: test.bat

Sidenote:
1) It is as i've suspected above (start creates own stdin/stdout for the new command shell).
2) With the missing last doublequote a file named "testOut.txt & exit" is created (in this test case);
so after testing the above you might also have such an useless file.
Sorry for that, i hope it now works as you need it.


penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running/Killing an executable after 20 minutes

#10 Post by foxidrive » 14 Dec 2015 09:13

Thanks for investigating that penpen, I wasn't aware of it either.

There may be a more elementary solution when double quotes aren't required, as this works here.

Code: Select all

start "" cmd /c "sort < file.txt >file.out"



So GilesTRAbbit can try using this line for the gulp command:

Code: Select all

start "" cmd /c "gulp.exe <input-%%a.dat >output-%%a.out"

Post Reply