.bat to check if a window or process is open or running and if it is do this if it is not do something else - windows

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

Related

Close and restart a process after a certain time period in batch

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 want to run a batch script into my pc when it is locked

i want to run a batch file on my pc for an overnight activity. The problem is that i can't do that because the pc is locked.
I am using this code:
#echo off
tasklist /FI "IMAGENAME eq \\Desktop\notepad.exe" | find /i "\\Desktop\notepad.exe"
IF ERRORLEVEL 1 GOTO LOOP1
IF ERRORLEVEL 0 GOTO EXIT
:LOOP1
start notepad.exe
goto EXIT
:EXIT
and it works only the pc is unlocked.
Any help will matters.
Create a new Scheduled Task. Set the task to run when user is logged in or not. Then set the interval of your task time.
On Windows 8 etc you are able to set triggers on when the task should be kicked of by using either a set time or when PC goes on idle, when an event occurs etc.
There is also an option for On Workstation Lock
If this is not your intention to use scheduler. Then right a script that runs in a permanent loop by adding some sleep time and only rerun the taks every now and again, something like this (untested, just used yours as example)
:START
#echo off
tasklist /FI "IMAGENAME eq \\Desktop\notepad.exe" |find /i "\\Desktop\notepad.exe"
IF ERRORLEVEL 1 GOTO LOOP1
IF ERRORLEVEL 0 GOTO EXIT
:LOOP1
start notepad.exe
timeout 300
goto START
timeout 300 is basically sleeping the script for 300 seconds and will start from START again. We can then run the batch file before locking the PC and it wil lrun in a continuous loop. Even though it might not be the right way, it is an option. Perhaps some more detail around how often the batch file should run?

Windows Command Line wait for task end before next command

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).

Check how many of a program are running via batch

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...)

Batch script to kill a process if not responding in a for loop

I am trying to install some windows standalone update files and to do this I need to use wusu.exe. Every now and again wusu.exe will hang. I have created a batch file called prereqs.bat and in this file I have calls to the wusu.exe. I need the code to kill wusu.exe if it hangs, and retry it again.
This is my code as it stands now:
:PreReqs32
taskkill /im prereqs32.bat /f
taskkill /im wusa.exe /f
when it loops back, it kills both the batch file and wusa.exe
start cmd /k c:\windows\temp\prereqs.bat
An outside process so that I can kill wusu.exe if things go awry.
timeout /t 240 /NOBREAK
this timeout is to wait until the install is complete which is sometimes not enough.
taskkill /im "[wusa.exe]" /fi "STATUS eq NOT RESPONDING"
if "%ERRORLEVEL%"=="1" goto PreReqs32
Is there a way for some sort of FOR loop, with logic to exit if the status is not "not responding"?
Also, as a bonus, is there a way to forgo the timeout and just "wait" for prereqs.bat to be complete before moving on, assuming that wusu.exe has not hung?

Resources