Is it possible to check how many of a program are running via a batch file?
I made a program to use for tab for a cause and this is the code:
:1
timeout /t 2
start chrome.exe
start chrome.exe
start chrome.exe
start chrome.exe
timeout /t 7
taskkill /f /im chrome.exe
goto :1
But this program sometimes open more than 4 copies of chrome before closing them, up to 8. Is there a command that I could use which will say how many of the program are open? kinda like the below:
#echo off
:1
start chrome.exe
if "4 of chrome.exe are open" goto :2 else goto :1
:2
timeout /t 7
taskkill /f /im chrome.exe
timeout /t 2
goto :1
I kept on getting errors saying that it is not formatted properly until I put in all the spaces where the code is, why do you have to do that?
It's difficult to treat chrome's processes because it normally creates a separated process for each plug-in (add-on). But answering your question:
Is there a command that I could use which will say how many of the program are open?
Yes, you can see how many processes with same name are open at the moment:
tasklist|find /c "chrome.exe"
For using this within an if statement you can do the following:
for /f %%a in ('tasklist^|find /c "chrome.exe"') do (set chromeProcesses=%%a)
if "%chromeProcesses%" == "4" (do something...)
Related
I need a little help here.
I need to create a .bat file to check if a process is running every 5 seconds. If the process is running then it simply resets and checks again in 5 seconds. If the process is not running, then it closes all other .bat files and restarts everything.
This is for a game server. So right now the whole process includes a few .bat files of which do different things. The initiate.bat starts the process by starting startserver.bat. startserver.bat then starts the game server, it's window opens and the server starts, it the starts the .bat file schedule.bat which sets a timer and is meant to restart the server once the timer has run out and then startserver.bat starts up anticrash.bat of which the code for it is below.
I have a feeling the code is not finding the server window or the process. I've double checked the process file name and have made sure that is correct. The title in the window for the server is simply the path to the .exe, but no matter what I've tried I can't get it to function correctly. It always ends up in a loop and not identifying the open or closed process and it simply defaults to "Server was not found!". If I swap the code it will simply loop "Server was found!" and proceed that way.
This is not the only example I've tried either. I've looked at literally every article I could find on how to check if a process or window was open. I've tried to add a title to the window and search for the title, I've tried other code configurations and if then statements and nothing works.
In the end I'd like the .bat to simply check if the server process is running or if the window is open, either way as I don't care how it identifies it, and if the server window is open and the server is running then simply recheck every 5 seconds. If it finds that the process or window was closed or ended, then it should close all other .bats and restart the process of initiating the server. It's mainly for when the server crashes I want it to restart if it does.
#echo off
goto check
:check
cls
echo Starting Anit Crash... Check every 5 Seconds...
SETLOCAL EnableExtensions
set EXE=program.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Server was not found!
echo Server may have crashed...
timeout /t 5
echo Shutting Down Other Processes First...
taskkill /f /im "schedule.bat"
echo Server Restarting...
start cmd /k call initiate.bat
echo Exiting Anti Crash...
timeout /t 3
goto FIN
:FOUND
echo Server Was Found...
echo Server Is Active... Rechecking In 5 Seconds...
timeout /t 5
goto check
:FIN
exit
It may solve your Problem
#echo off
:values
set EXE=safeincloud.exe
:check
cls
echo Starting Anti Crash... Check every 5 Seconds...
tasklist /fi "ImageName eq %EXE%" /fo csv 2>NUL | find /I "%EXE%">NUL
if "%ERRORLEVEL%"=="0" goto FOUND
echo Server was not found!
echo Server may be crashed...
timeout /t 5 >nul
echo Shutting Down Other Processes First...
taskkill /f /im "schedule.bat"
echo Server Restarting...
start "" "initiate.bat"
echo Exiting Anti Crash...
timeout /t 3 >nul
goto End
:FOUND
echo.
echo Server Was Found...
echo Server Is Active... Rechecking In 5 Seconds...
timeout /t 5 >nul
goto check
:End
exit
Source : How to check if a process is running via a batch script
I'm trying to make a batch file that opens a certain exe and then, after (for example) 5 minutes, closes it then reopens it again. I have tried this:
#echo off
:loop
cd /d %~dp0
certain exe
timeout /t "time" (by minutes)
taskkill /f /im "certain exe"
goto loop
but it wouldn't close the exe nor open it, what can I do?
The problem is that cmd will wait for the current command to end before executing the next command, so timeout (and taskill) will not be executed until example.exe closes by itself.
To ensure that cmd doesn't wait example.exe, you'll need to use start /B, as in this example:
#echo off
:loop
start /B "example.exe"
ping -n seconds_of_delay+1 127.0.0.1>nul
taskkill /f /im "example.exe"
goto :loop
Replace seconds_of_delay+1 with the number of seconds you want the time period to last plus one.
/B is needed because, without it, start would execute example.exe in a new window (if you want that, simply use start).
I used ping as the way to create a time delay because it has been found to consume less processor time than sleep or timeout (you can find more details here).
I have some software for Windows that opens a website on exit. I want to run a batch that closes the browser after exit from example.exe
example.exe
(wait for end of example.exe)
taskkill /im firefox.exe
taskkill /im iexplore.exe
taskkill /im chrome.exe
The problem you are observing is probably that at the end of your game, the browser is started as another process. It can then take a while before the browser actually starts but the game doesn't wait and just exits. So when you come at the taskkill commands, the browser is still not open. What you need is a way to verify if a browser-process already started, not a way to find out if the exemple.exe (or your game) exited or not. You can do it this way:
:waitOnBrowser
tasklist | findstr /B /C:"chrome.exe" /C:"firefox.exe" /C:"iexplore.exe" > nul
REM the errorlevel will only be 0 if one of the browser has been found in the tasklist
IF ERRORLEVEL 1 goto :waitOnBrowser
REM At this point, you're sure at least one browser has been started
REM taskkill can come here
IF ERRORLEVEL 1 is kind of the same as saying IF %ERRORLEVEL% GTR 1 (see IF in batch).
I am programming a simple program that will close a process the the user inputs.
For example i will write Chrome , explorer , and then the program will close the chosen process.
My only problem is how do i actually close the chosen process aka p.
Code:
#echo off
#echo Thank you for using my program.
set /p id=Enter Process Name With .exe in the end:
taskkill p /im explorer.exe
timeout 5
%0
You may like to use this line:
taskkill /p /im %id%
or this one which forces the process closed, but it may corrupt data depending on which program is being forced closed.
taskkill /p /f /im %id%
I'm trying to find out how to limit a program execution time within a Windows batch file. Is there something like Unix timeout command?
Please advise.
To limit the time a program has to run you could do something like this
start yourprogram.exe
timeout /t 10
taskkill /im yourprogram.exe /f
That starts yourprogram.exe, waits 10 seconds, then kills the program.
I just installed Cygwin and use unix-style timeout command from the distribution.
I don't think there is a timeout command. However, you can start executing a task in the background and sleep (using ping) for the timeout duration then kill the task.
This code waits 60 seconds, then checks to see if %ProgramName% is running.
To increase this time, change the value of WaitForMinutes.
To decrease the interval between checks, set WaitForSeconds for the number of seconds you want it to wait.
#echo off
set ProgramName=calc.exe
set EndInHours=2
:: How Many Minutes in between each check to see if %ProgramName% is Running
:: To change it to seconds, just set %WaitForSeconds% Manually
set WaitForMinutes=1
set /a WaitForSeconds=%WaitForMinutes%*60
:: How many times to loop
set /a MaxLoop=(%EndInHours%*60*60) / (%WaitForMinutes%*60)
REM Use a VBScript popup window asking to terminate %ProgramName%
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo Wscript.Quit (WshShell.Popup( "Click 'OK' to terminate %ProgramName%." ,10 ,"Terminate %ProgramName%", 0)) >> %tmp%\tmp.vbs
start %ProgramName%
set running=True
:: Give time for %ProgramName% to launch.
timeout /t 5 /nobreak > nul
setlocal enabledelayedexpansion
for /l %%x in (1,1,%MaxLoop%) do (
if "!running!"=="True" for /l %%y in (1,1,%WaitForMinutes%) do (
if "!running!"=="True" (
set running=False
REM call Pop-Up
cscript /nologo %tmp%\tmp.vbs
if !errorlevel!==-1 (
for /f "skip=3" %%x in ('tasklist /fi "IMAGENAME EQ %ProgramName%"') do set running=True
) else (
taskkill /im %ProgramName%
)
)
)
)
if exist %tmp%\tmp.vbs del %tmp%\tmp.vbs
This code uses VBScript to make a pop-up box. Clicking OK will cause %ProgramName% to be killed via taskkill.
If you do not want to use a pop-up window, you can use timeout by removing...
REM Use a VBScript popup window asking to terminate %ProgramName%
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo Wscript.Quit (WshShell.Popup( "Click 'OK' to terminate %ProgramName%." ,10 ,"Terminate %ProgramName%", 0)) >> %tmp%\tmp.vbs
...and replacing this...
REM call Pop-Up
cscript /nologo %tmp%\tmp.vbs
if !errorlevel!==-1 (
...with this:
REM Use CTRL+C to kill %ProgramName%
timeout /t %WaitForSeconds% /nobreak
if !errorlevel!==0 (
Using /nobreak is necessary because timeout does not distinguish between pressing a key or timing out. This will allow you to terminate %ProgramName% by pressing CTRL+C , but that causes your batch file to ask Terminate batch job (Y/N)? when you do. Sloppy/Messy/Nasty IMHO.
You could instead use CHOICE by replacing the above mentioned code with this:
REM Using choice, but choice can get stuck with a wrong keystroke
Echo [K]ill %ProgramName% or [S]imulate %WaitForSeconds% Seconds
Choice /n /c sk /t %WaitForSeconds% /d s
if !errorlevel!==1 (
But choice brings it's own set of limitations to the table. For one thing, it will stop it's countdown if a key that is not among it's choices (in this case s and k) has been pressed, essentially locking up until a correct choice is made. Second, the SPACEBAR cannot be a choice.