I currently have this batch script:
start udp%1.exe
timeout /t %2
taskkill /f /im "udp%1.exe"
This works great and kills the program after a selected amount of time, however if I was to manually close udp%1.exe and start another one with the same ID the timer from the other batch script is still running and will kill the next launched. I'm not entirely sure if I can check if the process is still running in batch? and if so to close the timer so it doesn't kill others with the same ID?
Try this code:
#echo off
start "udp%1.exe"
timeout /t %2
tasklist /FI "IMAGENAME eq udp%1.exe" 2>NUL | find /I /N "udp%1.exe">NUL
if "%ERRORLEVEL%"=="0" goto yes
goto no
:yes
taskkill /f /im "udp%1.exe"
exit
:no
exit
It waits the selected amount of time and if the program is running, it closes it, and if the program is not running, it exits. If you wan't to detect if the program is closed while the timer is still running, you might want to look into vbs scripts. :)
Related
I've got a piped TASKLIST | FINDSTR command, which can take long time to finish. I don't want to wait too long for it, so I want to stop searching with FINDSTR and just get ERRORLEVEL 1 value, after for example 5s.
This script got to look if the specific EXE is running, and if it's not - open it.
#echo off
:B
set Poro=CapsuleFarmerEvolved.exe
TASKLIST /fi "imagename eq %Poro%" 2>nul | FINDSTR /i %Poro%
if ERRORLEVEL 0 (GOTO :B) else (start "" CapsuleFarmerEvolved.exe)
GOTO :B
I've tried adding timeout before the TASKLIST, but since it's piped, it doesn't work.
I was wondering maybe about creating a different .bat with this exact command and somehow kill the whole process, then return the value to the main script.
That is my first take to create a script, so please forgive me lack of basic knowlege, but I'd tried my best before posting this
This should do it, however this is still a ridiculous idea, i.e. using a never-ending script to monitor, and restart an application, when it is no longer running.
#Echo Off
Set "Poro=CapsuleFarmerEvolved.exe"
:Loop
%SystemRoot%\System32\tasklist.exe /Fi "Status Eq Running" /Fi "ImageName Eq %Poro%" 2>NUL | %SystemRoot%\System32\find.exe /I "%Poro%" 1>NUL || Start "" "P:\athTo\%Poro%"
%SystemRoot%\System32\timeout.exe /T 5 /NoBreak 1>NUL
GoTo Loop
I wanted to close opened app by running a batchfile code . My current code does not work and just freezes all black.
Code
#echo off
echo closing all programs...
taskkill /f /t /im explorer.exe
explorer.exe
I think taskkill /f /im explorer.exe should work oh and you should do pause or pause >nul at the end of the script else your program won't start also explorer.exe at the end of your script is not needed.
I have bitlord I want this program to run when i click a batch file and when i don't want this program to run i want to kill it with same batch file. Can anyone please write the script Thanks a lot for giving your time.
I tried it scripting but failed.
If bitlord is not running then this will launch it
If bitlord is running then this will kill it
Change line 2 and line 3 to set the program name and path.
#echo off
set "program=bitlord.exe"
set "folder=c:\program files\bitlord"
tasklist |find /i "%program%" >nul || start "" "%folder%\%program%" & goto :EOF
tasklist |find /i "%program%" >nul && taskkill /f /im "%program%" & goto :EOF
Okay. I think i understand your question now:
#echo off
taskkill.exe /f /im notepad.exe
start calc.exe
The magic is to use the start command which starts a new command asynchronous. Meaning, it doesn't wait for the termination.
I am running a program (Win7) which sometimes gets stuck and not responding.
Just wanted your help to write batch process that can check the program status and if it's Not responding then close and open it again
Test this: it should only relaunch be2beat when the task is not responding, and has been killed forcefully.
#echo off
taskkill /im "Be2Beat.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && start "" "C:\Program Files (x86)\Be2Beat\Be2Beat Multimedia Platform\Be2Beat.exe"
This will run it on a continuous loop to monitor.
Create file 2.bat
#echo off
:loop
taskkill /im "Be2Beat.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && start "" "C:\Program Files (x86)\Be2Beat\Be2Beat Multimedia Platform\Be2Beat.exe"
goto loop
Basically I have a simple batch script just now that starts a program multiple times let's call it 1.exe, 1.exe will launch 20 times and then be killed after % amount of seconds (specified by a command line argument).
What I need done is if 1.exe is already running, launch 2.exe instead, if 2.exe and 1.exe are already running launch 3.exe instead and so on.
All the code I really have just now is
Timeout /t 20 /nobreak >nul
echo.
taskkill /F /IM %programname%
echo.
pause >nul
and also the launching of the 1.exe
Any help is appreciated.
The Batch code below launch 2.exe if 1.exe is already running, launch 3.exe if 2.exe is already running and so on:
for /L %%i in (1,1,20) do (
for /F "skip=3" %%e in ('tasklist /fi "imagename eq %%i.exe"') do (
if "%%e" equ "%%i.exe" set lastExe=%%i
)
)
set /A lastExe+=1
%lastExe%.exe