detect crashed application with .bat - batch-file

Hi guys is it possible to detect application crash with .bat code then restart that with .bat.
pleas give me a method.
i write a code to start and restart application but i don't know how to detect crashed application.
thank you
#echo off
cls
echo Protecting NpAPIserver from crashes...
title NpAPIServer Watchdog
tasklist /FI "aplication.exe" | find /i "aplication.exe"
for /f "tokens=2" %%x in ('tasklist ^| findstr aplication.exe') do set PIDTOKILL=%%x
IF ERRORLEVEL 2 GOTO KILLPROGRAM
IF ERRORLEVEL 1 GOTO LAUNCHPROGRAM
:KILLPROGRAM
taskkill /F /PID %PIDTOKILL%
goto LAUNCHPROGRAM
:LAUNCHPROGRAM
cd "c:\np"
start aplication.exe
goto WAITLUNCH
:WAITLUNCH
timeout /T 300
for /f "tokens=2" %%x in ('tasklist ^| findstr aplication.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto LAUNCHPROGRAM

(tasklist /FI "Status eq not responding"|find "lol.exe")&&rem not reponding

Related

Test if process is running on remote units and output to log

We would like to check if a certain process is running on any of our listed servers, outputting the result to a log file like this:
SERVERNAME Process is running
SERVERNAME Process is not running
I'm new to batch but this is how far I got:
FOR /F "TOKENS=*" %%A IN (LIST.TXT) DO TASKLIST /S %%A /FI "IMAGENAME EQ IEXPLORE.EXE" >> ECHO %%A D:\SEARCH.LOG
This should pretty much do what you want.
#echo off
setlocal enabledelayedexpansion
for /F "delims=" %%a in (list.txt) do (
tasklist /s %%a | find /I "iexplore.exe" >nul
if !errorlevel! equ 0 (echo %%a Process is Running) else (echo %%a Process is Not Running)
) >> d:\search.log
As for your requirement on RPC errors, perhaps consider rpcping and echo to file if not available.
#echo off
setlocal enabledelayedexpansion
for /F "delims=" %%a in (list.txt) do (
rpcping -s %%a |find /i "Completed" >nul
if not !errorlevel! equ 0 echo %%a RPC Server not available
tasklist /s %%a | find /I "iexplore.exe" >nul
if !errorlevel! equ 0 (echo %%a Process is Running) else (echo %%a Process is Not Running)
) >> d:\search.log
What you have, isn't far away, to get the output you asked for however, you could change your script more like this:
#ECHO OFF
(FOR /F "TOKENS=*" %%A IN (LIST.TXT
) DO TASKLIST /S %%A|FIND /I "IEXPLORE.EXE">NUL&&(Echo %%A Process is running
)||Echo %%A Process is not running)>D:\SEARCH.LOG

Batch file check if a program is running only once and close duplicate instances or launch it if none is running

I'm trying to realize a batch file that checks if a program is running and if not launch it, and more if is it launched more than one time close all the duplicate instances.
this is the code I implemented using also some tips found here in stackoverflow:
:check
tasklist /fi "imagename eq notepad.exe" /nh |find /i /c "notepad.exe" > "%temp%\variable.txt" #here I count how many instances are running
set /p value=<"%temp%\variable.txt" #and save the value in the variable.txt file
##check and choose action
if %value% equ nul goto none
if %value% geq 2 goto more
if %value% equ 1 goto one
:more
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto check
:none
start notepad.exe
goto check
:one
timeout 10 > nul
goto check
But some strange behavior happens when I test it...
if only one instances is running all fine, but if I close notepad while the batch file is running the routine goes to the :more label apparently without any reason...what I'm doing wrong?
thanks for any help
stupid error ... this is the working code:
:check
timeout 10 > nul
tasklist /fi "imagename eq notepad.exe" /nh |find /i /c "notepad.exe" > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 0 goto none
if %value% geq 2 goto more
if %value% equ 1 goto check
:more
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto check
:none
start notepad.exe
goto check

Break infinite loop with taskkill

I trying to do an infinite loop to check if node.exe is using over some number of memory. Like this:
:loop2
sleep
taskkill /f /im node.exe "memusage gt 85000" > nul
timeout /t 30
GOTO loop2
But how can I break this infinite loop, when the taskkill is processed?
set "Myprocess=node.exe"
:test
tasklist | find /i "%MyProcess%">nul && Taskkill /F /IM "%MyProcess%" & exit/b
timeout /t 30
goto:test
:loop2
sleep
( taskkill /f /im node.exe /fi "memusage gt 85000" | find "PID" >nul ) || (
timeout /t 30
GOTO loop2
)
Check the output of the command to determine if a process has been killed.
Edited I thought the PID string was locale independent, but it is not. If needed, the find command can be replaced by
find /i "node.exe" > nul
to search for the name of the process in the output, or
findstr /r /c:" [1-9][0-9]*[02468][\., ]" > nul
to search for the presence of a process ID (note: all process IDs are multiple of 4)
Works for me, for any locale.
:loop2
sleep
set _tk=
for /f "skip=1 delims=, tokens=2" %%A in ('tasklist /fi "imagename eq node.exe" /fi "memusage gt 85000" /fo csv') do (
taskkill /f /pid %%~A
set _tk=1
)
if not "%_tk%"=="1" (
timeout /t 30
GOTO loop2
)
Another approach would be to use TASKLIST to see if a matching image is running. This code is -not- tested.
:loop2
SET TEMPFILE=%TEMP%\tk_%RANDOM%.tmp
TASKLIST /FI "IMAGENAME eq cmd.exe" /FI "MEMUSAGE gt 9000" >"%TEMPFILE%"
SET /P TKI=<"%TEMPFILE%"
IF "%TKI%" EQU "INFO: No tasks are running which match the specified criteria." (GOTO OutOfIt)
taskkill /f /im node.exe "memusage gt 85000" > nul
timeout /t 30
GOTO loop2
:OutOfIt
IF EXISTS "%TEMPFILE%" (DEL "%TEMPFILE%")
EXIT /B
taskkill /IM notepad.exe /FI "memusage gt 999" |find "SUCCESS" &&GOTO :BREAKOUT

How to list a process that contains spaces in the name

I'm making a batch that checks whether the process is active and if it is closed it ... the point is that when the process has spaces in the name it is not found.
Set "EXE=Process name.exe">nul 2>&1
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% Taskkill /F /IM "%EXE%" >nul 2>&1
This should handle the spaces in the code, depending on what the first tasklist prints:
Set "EXE=Process name.exe"
FOR /F "delims=" %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF /i "%%x" == "%EXE%" Taskkill /F /IM "%EXE%" >nul 2>&1
This is another way of doing it:
Set "EXE=Process name.exe"
tasklist|find /i "%EXE%" >nul && Taskkill /F /IM "%EXE%" >nul 2>&1
and this is functionally equivalent: it will only kill the task if it exists.
Set "EXE=Process name.exe"
Taskkill /F /IM "%EXE%" >nul 2>&1
wmic process where name="Process name.exe" get Name,ProcessId
?
Try, putting quotes around your process name like
Set EXE="Process name.exe" >nul 2>&1

batch file to check if exe is running if so taskkill

I'm having problems with the firefox Flashplayerplugin eating up too much ram and lagging my system when it's not in use. The only solution I found was killing the flashplayerplugin while using firefox, uninstalling, reinstalling or a fresh firefox install or new profile doesn't solve it; however, it's becoming very tedious having to check taskmanager all the time and kill it and the flashplayerplugin always seems to start on it's own.
The question I have is if it's possible to create a batch file to check if FlashPlugin_11_8_800_94.exe is running and kill it after a period of time (5-10 seconds) and continue running the batch file actively, in a loop, scanning if FlashPlugin_11_8_800_94.exe has started again, then kill it after 5 - 10 seconds, rinse and repeat?
Edit:
Found a batch file and modified it, but also seems to be missing some perimeters to actively search if it's running, even when it is not. It doesn't work either way though.
#echo off
:search
TASKLIST|FIND "FlashPlayerPlugin"
IF %ERRORLEVEL% = 0 THEN (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im FlashPlayerPlugin_11_8_800_94.exe
--
This batch file doesn't work either.
set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe
-------------------------------------------------------
:STOPPROC
set wasStopped=0
set procFound=0
set notFound_result=ERROR:
set procName=%1
for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
if NOT %%A==%notFound_result% (set procFound=1)
)
if %procFound%==0 (
echo The process was not running.
goto :EOF
)
set wasStopped=1
set ignore_result=INFO:
:CHECKDEAD
"%windir%\system32\timeout.exe" 3 /NOBREAK
for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
if not %%A==%ignore_result% (goto :CHECKDEAD)
)
goto :EOF
-------------------------------------------------------
:MAIN
call :STOPPROC FlashPlayerPlugin_11_8_800_94.exe
taskkill /im FlashPlugin_11_8_800_94* /f >nul 2>&1
For anyone who still might find it useful:
This is a small script that scans tasklist for processes containing processname once every 5 seconds. For example, if you put "notepad" for the processname, it will end processes like "notepad.exe" and "notepad++.exe". To use the script, copy and paste the following into notepad and save has "simple_pk.cmd". processname can have any characters except double quotes("), ampersands (&), or commas (,).
::Simple monitor and kill process
#echo off&prompt :&mode con cols=50 lines=10
set processname=flashplayerplugin
:loop
cls&echo Searching for %processname%...
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /I /C:"%processname%"') do call :killprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop
:killprocess
echo. |set /p d=killing %*...
taskkill /f /im "%*">nul 2>&1
set err=%errorlevel%
set success=Success
if not %err%==0 set success=fail (err code: %err%)
if %err%==128 set success=fail (process not found)
echo %success%&goto :eof
This is a slightly different version of the same script. This will only end processes that match the whole name exactly:
::Simple monitor and kill process (exact name)
#echo off&prompt :&mode con cols=50 lines=10
set processname=FlashPlayerPlugin_11_8_800_94.exe
:loop
cls&echo Searching for %processname%...
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /C:"%processname%"') do call :killprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop
:killprocess
set name=%*
set name=.,;%name:"=%;,.
echo %name%|FINDSTR /C:".,;%processname%;,.">nul || goto :eof
echo. |set /p d=killing %*...
taskkill /f /im "%*">nul 2>&1
set err=%errorlevel%
set success=Success
if not %err%==0 set success=fail (err code: %err%)
if %err%==128 set success=fail (process not found)
echo %success%&goto :eof
Your IF is not correct:
#echo off
:search
TASKLIST|FIND "setup.exe"
IF %ERRORLEVEL% equ 0 (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im setup.exe
More simpler form
#echo off
:search
TASKLIST|FIND "setup.exe"
IF %ERRORLEVEL% equ 0 (taskkill /im setup.exe
exit)
TIMEOUT /T 5
GOTO search
I was looking over the first bit of code at top and I find a way, I believe. This is what I got; really simple.
#echo off
cls
:start
timeout /t 5
tasklist|find "explorer.exe"
goto found
goto start
:found
taskkill /f /im explorer.exe
goto start
Of course, any program will work.

Resources