0

I am running a command in command prompt and I want to kill it after 15 minutes. I don't care if it is completed or not. I don't want to have any human interaction to kill the running command such as someone has to press CTRL+C to kill it.

Is there a way to do it.

Please note I don;t want to use any third party tools or scripts.

Akash
  • 179
  • 1
  • 3
  • 12
  • 2
    This seems like a duplicate of this question: http://stackoverflow.com/questions/11121424/need-to-run-an-exe-then-kill-it-after-10-seconds-and-move-to-next-command-in?rq=1 –  Jun 05 '15 at 21:27
  • Possible duplicate of [Need to run an .exe, then kill it after ~10 seconds and move to next command in batch file](http://stackoverflow.com/questions/11121424/need-to-run-an-exe-then-kill-it-after-10-seconds-and-move-to-next-command-in) – Andreas Apr 20 '17 at 11:43

4 Answers4

3
start forfiles /s
timeout /t 5
Taskkill /im forfiles.exe /f

is one way.

2

Did you mean something like that ?

@echo off
Tracert www.google.com
timeout /t 900 /nobreak >NUL
Taskkill /im cmd.exe /f

Edit : based on the blueray's comment :

how can I put this command in a single line to use in CMD?

Tracert www.google.com & timeout /t 900 /nobreak >NUL & Taskkill /im cmd.exe /f
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

May be this could help

start your-command-here

ping 127.0.0.1 -n 120 > NUL

taskkill /im cmd.exe /f

Explanation:

start: this command starts executing your command

ping: it pings your local machine(ip : 127.0.0.1) for 120 sec and >NUL redirects the output to nowhere else the output of ping command will be displayed on the cmd screen

taskkill: it is used to kill any task

/im: image name of the process to be terminated. if the command is running on cmd then cmd.exe or any program that you need to kill.

Hope it helps.

0

Try this, works well for me:

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
@echo off

set _time=0
set _timeout=30

start myprocess.exe

:waitforprocess
set /A _time=_time+1
IF !_time! GTR !_timeout! goto TimedOut

rem TaskList will return the task list of the image as specific
rem or it will return INFO: No tasks are running.... so look for 
rem the INFO statement using FindStr. FindStr will return a errorlevel
rem of 0 if it found the string and a 1 if it did not, so use that 
rem to work out next steps. 
rem -------------------------------------------------------------------
tasklist /NH /FI "IMAGENAME EQ myprocess.exe" | findstr INFO 

rem ERRORLEVEL 1 = Did not find the string, so try again. 
IF ERRORLEVEL 1 (
    timeout /T 1 /NOBREAK > nul
    goto :waitforprocess
)
GOTO DONE

:TimedOut
ECHO We timedout so will kill the process
taskkill /FI "IMAGENAME EQ myprocess.exe" /T /F

:Done
echo finished
trevleyb
  • 98
  • 6