I am very new to batch files and am trying to create a script that checks if a process is running and alert if the process is/is not running. This is what I have gotten from google, but isn't working the way I am wanting.
tasklist /FI "IMAGENAME eq example.exe" 2>NUL | find /I /N "example.exe">NUL if "%ERRORLEVEL%"=="0" echo Program is running
If this is batch (as the attempt from the question indicates), according to [MS.Docs]: Using multiple commands and conditional processing symbols:
&& [...]
command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
Example:
C:\>(tasklist /FI "IMAGENAME eq svchost.exe" 2>NUL | findstr /I /N "svchost.exe" >NUL) && (echo Program running)
Program running
C:\>(tasklist /FI "IMAGENAME eq svchost1.exe" 2>NUL | findstr /I /N "svchost1.exe" >NUL) && (echo Program running)
You are trying to over complicate it. Copy the below into a batch file. Also use findstr instead.
tasklist /fi "imagename eq example.exe" | findstr /i "example.exe" >nul
If %errorlevel%==0 echo example.exe running.
If %errorlevel%==1 echo example.exe Not running.
This is even less complicated, there's no need to filter by ImageName when you're using Find to filter too:
TaskList|Find /I "example.exe">Nul&&(Echo Running)||Echo Not running
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 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)
echo off
:loop
tasklist /fi "imagename eq Notepad.exe" | find "INFO:" > nul
if errorlevel 1 goto loop
wordpad.exe
This is not working in XP. It works fine in Windows 7.
When notepad.exe does not exist in the task list, tasklist /fi "imagename eq Notepad.exe" dumps the "INFO:" line to stderr in Windows XP. You could redirect stderr to stdout with 2>&1, but it's easier just to find /i "notepad" instead.
On a side note, instead of doing if errorlevel 1 you could use conditional execution.
#echo off
setlocal
:loop
rem // Output nothing for the following code block.
>NUL 2>NUL (
rem // Make sure notepad is not running before continuing.
tasklist /fi "imagename eq notepad.exe" | find /i "notepad" && (
rem // Notepad is in tasklist. Sleep 1 second, then check again.
timeout /t 1 /nobreak || ping -n 2 localhost
goto loop
)
)
wordpad.exe
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.
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