Need help adding not responding check to batch file - batch-file

I am in need of help to adding a not responding check to my .bat and restarting a server application when it is not responding. This is what I have so far.
#echo on
tasklist /nh /fi "imagename eq javaw.exe" | find /i "javaw.exe" >nul && (
echo Server is running.
//
I need a not responding check right afterward here. If the program is not responding,
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
else
exit
//
exit
) || (
echo Server process not found.
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
)
pause>nul
It is necessary for me to kill both of these process because it doesn't end eachother when it crashes apparently.

You already managed to filter by imagename.
You can also filter by Status (Running / Not Responding / Unknown):
tasklist /nh /fi "imagename eq java.exe" /fi "status eq not responding"
will give you any java.exe, which does not respond.
EDIT
well - when I think about it: I would try:
tasklist /nh /fi "imagename eq javaw.exe" /fi "status eq running` |find /i "javaw.exe" >nul && (
echo Server is running
rem nothing to do
) || (
echo Server is not running or not responding
rem restart service
)

Related

How can I make a batchfile that constantly checks if a program is open and if it is not, start it minimized

I am fairly new to coding and batch files, so bare with me.
The program I want to start that way is opera. But the batch file doesnt seem to find it. This is how far I got:
tasklist /FI "opera.exe" 2>NUL | find /I /N "opera.exe">NUL
if NOT "%ERRORLEVEL%" == "0" start "" "C:\Users\leonv\AppData\Local\Programs\Opera.exe"
PAUSE
I would take the verification check, just a little bit further. I would look to see if an ImageName of Opera.exe, with a Status of Running, and for the current UserName is returned:
#%SystemRoot%\System32\tasklist.exe /Fi "Status Eq Running" /Fi "ImageName Eq Opera.exe" /Fi "UserName Eq %UserDomain%\%UserName%" | %SystemRoot%\System32\find.exe "="
#If ErrorLevel 1 Start "" /Min "%LocalAppData%\Programs\Opera.exe"
You need to specify IMAGENAME eq processname like:
tasklist /fi "IMAGENAME eq opera.exe"
Additionally, no need to run if statements, you can use conditional operators && and ||
(tasklist /fi "IMAGENAME eq opera.exe"| findstr /I "opera.exe")>nul && echo It's running || start "" "%LocalAppData%\Programs\Opera.exe"
and to run it in a loop, checking every N of seconds:
#echo of
:loop
(tasklist /fi "IMAGENAME eq opera.exe"| findstr /I "opera.exe")>nul && echo It's running || start "" "%LocalAppData%\Programs\Opera.exe"
timeout /t 20
goto :loop

Batch file that identifies if a named window is open, and then closes it

I have been using the following script to check if a particular named window is open.
I got it from this thread:-
How do you test if a window (by title) is already open from the command prompt?
ideally I will expand the else part to close the window if it is found to be open.
#For /f "Delims=:" %A in ('tasklist /v /fi "WINDOWTITLE eq test.bat - Notepad"') do #if %A==INFO (echo Prog not running) else SET "BREX=Awesome" &echo %BREX%
Unfortunately when I run this script it returns three instances of my else string?
Is there any way to reduce this down to returning one instance?
You could use findstr instead. You're getting multiple lines of output as you're looping over each line of output
tasklist /v /fi "WINDOWTITLE eq test.bat - Notepad" | findstr /C:"No tasks are running"
if %errorlevel% NEQ 0 (
echo awesome
) else (
echo Prog not running
)
If you really want to do this with one line from the cmd prompt you can do this.
cmd /v:on /c "#For /f "Delims=:" %A in ('tasklist /v /nh /fi "WINDOWTITLE eq test.bat - Notepad"') do #if %A==INFO (echo Prog not running) else (SET "BREX=Awesome") &echo !BREX!"
Or use some conditional execution.
tasklist /v /nh /fi "WINDOWTITLE eq test.bat - Notepad" |findstr /B /C:"INFO: No tasks are running">nul && (echo Program not running) || (echo Awesome)

Make a .batch file to start/stop a program?

I need a batch (.bat) file that opens a program if it's not open, and stops the program if it is open. I have a game where when the launcher is closed, it stays open in the background. And I have to end it with task manager or else I can't launch it because steam doesn't like it when an app is open two times (it doesn't allow it), so I would like a batch file that does this for me, then bind it to a macro.
To check if your programm is running : (Here an example with notepad.exe)
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && echo %MyProcess% Is running || echo %MyProcess% Is not running
So you can do like that :
#echo off
Set "MyProcess=Notepad.exe"
tasklist | find /i "%MyProcess%">nul && Taskkill /F/IM "%MyProcess%" || start "%MyProcess%"
This is another way to do it:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find "." > nul && taskkill /f /im "Launcher.exe" & goto :EOF
tasklist /fi "imagename eq Launcher.exe" |find "." > nul || start "" steam://rungameid/243870 & goto :EOF
So I figured this out:
#echo off
tasklist /fi "imagename eq Launcher.exe" |find ":" > nul
if errorlevel 0 taskkill /f /im "Launcher.exe" && exit
start steam://rungameid/243870
exit
The logic behind it is that if you can kill the process, then end the prompt before you can create a new session of the process. But if the process can't be killed, then make a new session of it, then end the prompt.

Close all firefox tabs with BATCH

I try to close all firefox if there are more than one. I want to do loop that check the firefox process and close and check again until there is no any firefox process.
This is my code:
:loop
taskkill /im "firefox.exe"
tasklist /fi "imagename eq firefox.exe" goto loop
Where is my wrong in this command ? Any help is appreciated.
:loop
tasklist /fi "imagename eq firefox.exe" | find "firefox.exe" >nul && ( taskkill /im firefox.exe >nul & goto loop )
Get list of tasks, and if firefox.exe included in it, then kill it and goto loop
You can also use the following to force kill FF windows:
taskkill /F /FI "ImageName eq firefox.exe"
One taskkill command will kill every firefox process - unless it is not responding.
You could use two taskkill commands separated by a delay of several seconds, and the second one should use the /f force switch.
I do
#echo off
TASKKILL /IM firefox.exe /F
pause>nul
exit
I'm on windows 7, it works for me

Scheduled batch script stuck on running

I have a script while ran from a CMD runs normally, however when I run it from task scheduler although it does everything as intended, the script status hangs on "Running"
Here is the script:
#echo OFF
tasklist /FI "IMAGENAME eq utorrent.exe" 2>NUL | find /I /N "utorrent.exe">NUL
if "%ERRORLEVEL%"=="0" (echo UTorrent is running nothing to do) ELSE (
echo UTorrent is not running, starting Utorrent!
start C:\Users\Adonis\AppData\Roaming\uTorrent\uTorrent.exe
)
tasklist /FI "IMAGENAME eq steam.exe" 2>NUL | find /I /N "steam.exe">NUL
if "%ERRORLEVEL%"=="0" (echo Steam is running nothing to do) ELSE (
echo Steam is not running, starting Steam!
start X:\Games\SteamLibrary\Steam.exe
)
exit
Can anyone advise why this is happening? IE why is the script stuck in running state through scheduler?
The OS in question is windows 8.
It is set to run only when user is logged on and with highest privileges.
Thanks!
Try to launch the application with the /B parameter to don't stop the execution of he script.
Also I've improved the syntax of your code:
#echo OFF
(Tasklist /FI "IMAGENAME eq utorrent.exe" | find /I "utorrent.exe">NUL && (
Echo: UTorrent is running nothing to do)
) || (
Echo: UTorrent is not running, starting Utorrent!
Start /B "" "C:\Users\Adonis\AppData\Roaming\uTorrent\uTorrent.exe"
)
(Tasklist /FI "IMAGENAME eq steam.exe" | find /I "steam.exe">NUL && (
Echo: Steam is running nothing to do)
) || (
Echo: Steam is not running, starting Steam!
Start /B "" "X:\Games\SteamLibrary\Steam.exe"
)
Exit

Resources