Kill slow command after some time and return value - batch-file

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

Related

Batch script to tell if process is running or not

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

Command to copy a file every 10 min only when Firefox is open

I would like to copy a file every 10 minutes when Firefox is open.
I am not sure what is the best way to do that but so far I have created a .bat that I wanted to link to a task in Task Scheduler that will run it every 10 minutes.
This is the command in the .bat :
copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
But Task Scheduler doesn't allow me to add a condition (= run only if firefox is open) nor to run a task every 10 minutes.
So here are my questions:
If a .bat is a good way to do this, how can I run a .bat with those two parameters?
If a .bat is not a good way to do this, what method would you recommend?
You could just have a batch file like this:
#echo off
:loop
tasklist /FI "IMAGENAME eq firefox.exe" 2>NUL | find /I /N "firefox.exe">NUL
if "%ERRORLEVEL%"=="0" copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
timeout /t 600 /nobreak
goto loop
This would be a standalone batch-script you would have to start yourself, or put in the startup folder.
You could also just take the
#echo off
tasklist /FI "IMAGENAME eq firefox.exe" 2>NUL | find /I /N "firefox.exe">NUL
if "%ERRORLEVEL%"=="0" copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
and run that every 10 minutes from task scheduler ofcourse.
You could also make this into your firefox shorcut and end it when the program isn't active anymore, like this:
#echo off
set programName=firefox.exe
start %programName%
:loop
tasklist /FI "IMAGENAME eq %programName%" 2>NUL | find /I /N "%programName%">NUL
if "%ERRORLEVEL%"=="0" copy "C:\Users\Me\Zotero\zotero.sqlite" "C:\Users\Me\Zotero\Zotero_Backup_sqlite\zotero_%date%.sqlite"
if "%ERRORLEVEL%"=="1" exit /b
timeout /t 600 /nobreak
goto loop

Batch File to Check for Running Program and Start if not Running is not working

I have been trying to get this batch file to work but keep running into issues. I think I am close but need help getting this working. When the script runs I get Find: Parameter format not correct.
I am running this on a Windows Server 2008 R2 Standard.
#echo off
tasklist /FI "IMAGENAME eq program.exe" | find /i “program.exe"
IF ERRORLEVEL 2 GOTO NEXTPROGRAM
IF ERRORLEVEL 1 GOTO LAUNCHPROGRAM
:NEXTPROGRAM
goto SMADMIN
:LAUNCHPROGRAM
start "" "C:\path\to\program.exe"
goto SMADMIN
:SMADMIN
tasklist /FI "IMAGENAME eq program1.exe" | find /i “program1.exe"
IF ERRORLEVEL 2 GOTO NEXTPROGRAM2
IF ERRORLEVEL 1 GOTO LAUNCHPROGRAM2
:NEXTPROGRAM2
goto COMPLETE
:LAUNCHPROGRAM2
start "" "C:\path\to\program1.exe"
goto COMPLETE
You can check whether the exe is running this way:
SET running=0
FOR /f "tokens=*" %%A IN ('tasklist^ /v^| findstr /i /c:"program.exe"') DO SET running=1
IF %running%=1 GOTO NEXTPROGRAM
IF %running%=0 GOTO LAUNCHPROGRAM
Afterwards you just have to check if the %ProgramRunning% is set to 1.
Don't forget to reset the %running% flag back to 0 before reusing it.

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.

Exit status of tasklist in batch file?

I am executing following command in a label inside a batch file:
tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" 2>nul && echo errorl:%errorlevel%
%1 is process running and %2 is its PID.
Even if process and its PID matches or doesnt matches, I m getting "errorl:1" in o/p.
I am not sure whats wrong here. Any idea?
You could pipe tasklist through the find command and get an errorlevel off of it.
Example:
tasklist | find "firefox.exe"
echo Error level = %ERRORLEVEL%
REM If firefox is running, the errorlevel is set to 0
REM If firefox is not running, errorlevel is set to 1
In my opinion, you can't use errorlevel at all,
because tasklist always returns a 0 even if the pid isn't found.
I suppose, you have to parse the output of tasklist.
To fetch the output of a command, FOR /F is a good choice.
To avoid problems wth the quoting in the FOR /F, I build first a cmd variable which is expanded with delayed expansion to avoid any side effects of special characters.
#echo off
setlocal enableDelayedExpansion
set "cmd=tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2""
for /F "delims=*" %%p in ('!cmd! ^| findstr "%2" ') do (
echo found %%p
)
%variables% are expanded before executing the line, so %errorlevel% will expand to some old value. (The fact that the code after && executes at all is your clue that the command returned 0)
You options are:
Use %errorlevel% or the more correct IF errorlevel 1 ... on the next line
Call setlocal ENABLEDELAYEDEXPANSION first and then use !errorlevel!
Edit:
I guess tasklist is buggy and/or stupid when it comes to exit codes, I wrote some code that does not use the exit code at all:
#echo off
if "%~1"=="SOTEST" (
start calc
ping -n 2 localhost >nul
for /F "tokens=1,2 skip=3" %%A in ('tasklist /FI "IMAGENAME eq calc.exe"') do (
call "%~0" %%A %%B
)
call "%~0" dummy.exe 666
goto :EOF
)
goto main
:IsTaskRunning
setlocal ENABLEEXTENSIONS&set _r=0
>nul 2>&1 (for /F "tokens=1,2" %%A in ('tasklist /FO LIST %*') do (
if /I "%%~A"=="PID:" set _r=1
))
endlocal&set IsTaskRunning=%_r%&goto :EOF
:main
call :IsTaskRunning /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2"
if %IsTaskRunning% gtr 0 (echo.%1:%2 is running) else (echo.%1:%2 is NOT running)
Run it as test.cmd SOTEST and it prints:
calc.exe:4852 is running
dummy.exe:666 is NOT running
Easy solution to this, given that
1) you can't get an errorlevel from tasklist, and
2) you can't directly pipe it to a FIND
Just write it to a file using output redirection and use FIND to check the file. Each time this is run, it will overwrite the previous iteration, so no need to even do any file cleanup. Amazing how many bat/cmd file limitations can be overcome with a simple scratchpad file!!
:TOP
rem swap rems from good to bad to test
set findvar=goodfile.exe
rem set findvar=badfile.exe
set scratchfile=scratch.txt
tasklist /fi "status eq running" /fi "imagename eq %findvar%">%scratchfile%
type %scratchfile%
pause
echo Looking for %findvar%
find "%findvar%" %scratchfile%
echo Error level = %errorlevel%
pause
IF errorlevel 1 GOTO BAD
IF errorlevel 0 GOTO GOOD
GOTO OTHER
:BAD
echo Errrlevel 1 - task not found
PAUSE
GOTO TOP
:GOOD
echo Errrlevel 0 - task is running
pause
GOTO TOP
:OTHER
echo something else ????? you blew it somewhere!
tasklist returns 0 when executes successfully:
If you're looking for existence of some process or some attribute of a process, one method is to supply the attributes to tasklist and check if it returned any process names. If no matching processes are found, it'll return "INFO: No tasks are running which match the specified criteria."
The result of tasklist may be checked either via for command embedding (and parse command output) or filter via find or findstr, which accepts regular expressions & wildcards.
eg. To check if the any process is running with following criteria:
tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" | find /v "No task" >nul && (echo process exists) || (echo na man).
Above method can also find if any document (window) is open, in addition to the underlying process, like "firefox.exe".
eg. close high speed vpn ad window if/when it pops up without notice:
tasklist /fi "windowtitle eq High-Speed*" | find /v "No task" >nul && (taskkill /fi "windowtitle eq High-Speed*")
Tested on Win 10 CMD

Resources