I am starting a program from Batch, but if the program is already running, I want to avoid starting a second instance.
So far, I have crafted the following code, which seems to work:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FIND "speedfan.exe" >NUL
IF %ERRORLEVEL%==0 (
TASKKILL /F /IM speedfan.exe
)
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
Alternatively, if I want to keep the current instance instead of starting a new one:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FIND "speedfan.exe" >NUL
IF NOT %ERRORLEVEL%==0 (
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
)
Can this be improved/simplified?
Update: Thank you all for your comments! So far, my revised code is becoming:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FINDSTR /BLI "speedfan.exe " >NUL || (
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
)
(and maybe I'll drop the /FI "IMAGENAME eq speedfan.exe" part for the sake of simplicity)
Here's a quick example intended to request input confirmation to close it, if it is running, or to just open it if isn't running. I hope it is robust enough for your purposes.
#"%__AppDir__%tasklist.exe" /Fi "ImageName Eq speedfan.exe" /Fo CSV /NH^
| "%__AppDir__%find.exe" ":" > NUL && (
Start "" "C:\Program Files (x86)\SpeedFan\speedfan.exe") || (
"%__AppDir__%choice.exe" /M "Would you like to close SpeedFan"
If Not ErrorLevel 2 "%__AppDir__%taskkill.exe" /Im speedfan.exe /T > NUL)
I have split the long first line over two for tidiness, and because you don't particularly like long lines.
Related
in my batch script,
tasklist /nh /fi "imagename eq udp-receiver.exe" | find /i "udp-receiver.exe" > 2 || (start c:\udp-receiver.exe -f 1.zip --nosync --interface 00-00-ab-00-00-CD --portbase 989)
(start d:udp-receiver.exe -f 1.zip --nosync --interface 00-00-ab-00-00-CF --portbase 989)
to open 2 session for udp recieve. I am require to use a loop to reopen the either 2 seesion if it close. how do i write the loop in batch file?
find /i "udp-receiver.exe" > 2
is incorrect. > is a redirection operator (output of the "find" will be redirected to the file "2").
If you want to count the number of lines containing a string, then you need to apply the /c switch to the find command.
for /f %%a in ('
tasklist /nh /fi "imagename eq udp-receiver.exe" ^| find /i /c "udp-receiver.exe"'
) do if %%a gtr 2 (echo more than 2) else (echo 2 or fewer)
may give you the results you seek.
so google didn't help me at all i need to ask here again.
I use this kind of method to check if my servers are running in 2 batch files.
tasklist /FI "IMAGENAME eq server_64.exe" 2> nul | find "server_64.exe" > nul
IF ERRORLEVEL == 1 (
echo Server is not running
echo.
) else (
echo Stopping Server ...
echo.
taskkill /F /IM server_64.exe > nul 2>&1
)
One to start and one to stop the servers.
Well this works great but when it comes to batch files it wont work for me...
I have one server which runs on phyton so start it via batch file.
My question is, is there a way to get somehow the batch file process status and stop it like it works for exe?
I hope i explained it good enough.
Thx in advance! :)
You can try it with a batch file like this :
#echo off
set "Process=server_64.exe"
Title Checking for status of this process ===^> "%Process%"
tasklist /nh /fi "imagename eq %Process%" 2>nul |find /i "%Process%" >nul
IF '%ERRORLEVEL%' EQU '1' (
Color 0B
echo.
echo "%Process%" is not running
) else (
Color 0C
echo.
echo Stopping "%Process%" ...
taskkill /F /IM "%Process%" > nul 2>&1
)
pause
Omg i found the solution, this was a beast...
tasklist /fi "imagename eq cmd.exe" /v /fo table /nh | find /i "Broker" 2>nul
but what is starmnge is that i cant get the output to be silnce...
when i try to mute it it gives me always error level 1.
tasklist /fi "imagename eq cmd.exe" /v /fo table /nh 2>nul | find /i "Broker" 2>nul
so whats wrong with this? ^
I made a batch file with the following script:
#echo off
goto start
:start
"C:\Games\SV_Debug\Data\Win32\InvisibleM2.vbs"
goto check
:check
tasklist /FI "IMAGENAME eq Launcher2.exe" >swap | find /I /N "Launcher2.exe" >swap
if "%ERRORLEVEL%" == "0" (
goto waitrecheck
) else "%ERRORLEVEL%" (
goto start
)
:waitrecheck
ping 127.0.0.1 -n 3
goto check
This batch file is executed by a vbscript that make's this batch invisible.
The batch file runs a vbscript that runs Launcher2.exe normal and not invisible.
But this batch above here can't check is Launcher2.exe is running.
When it tries I get this error:
The process does not have access to the file because it's being used by another program.
How do I fix this?
tasklist /FI "IMAGENAME eq Launcher2.exe" >swap | find /I /N "Launcher2.exe" >swap
You can not send the output of two programs to the same file at the same time
tasklist /FI "IMAGENAME eq Launcher2.exe" | find /I /N "Launcher2.exe" >nul
How would i go about finding if a version of Outlook is running as a specific user?
I need to check and then if it's not open it under that logged in account.
I've tried a few suggestions from around the site but none are care about the logged in user
Some examples of what i have tried
-------------------------------
tasklist /FI "IMAGENAME eq outlook.exe" 2>NUL | find /I /N "outlook.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
----------------------
tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end
start notepad.exe
:end
del search.log
-------------------------------------
I can't install anything on the server and so would need an existing windows solution.
Can't you just add another clause to your tasklist query like so?
tasklist /FI "IMAGENAME eq outlook.exe" /FI "USERNAME eq %username%"
EDIT
Full script that should work.
tasklist /FI "IMAGENAME eq outlook.exe" /FI "USERNAME eq %username%" 2>NUL | find /I /N "outlook.exe">NUL
if "%ERRORLEVEL%" == "1" start outlook.exe
I would like to use a batch file to compare the number of processes named "standard.exe", that are running on my Windows 7 machine, with the number of processes named "basic.exe". If the amount of processes called "standard.exe" equals the amount of processes called "basic.exe" nothing should happen, if the numbers are unequal, basic.exe should be restarted.
Any ideas? Already found the following code to check whether a process is running, but now I would like to count the number of processes carrying the same name.
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
Thanks in advance!
Using your example simply replace the /N in find with /C to return the count of processes.
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /C "myapp.exe"
Then you can just reduce it down to :
tasklist | find /I /C "myapp.exe"
Although as Andriy M points out it will match both myapp.exe and notmyapp.exe.
As for the second part of your question, simply do this:
set a=tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /C "myapp.exe"
set b=tasklist /FI "IMAGENAME eq myapp2.exe" 2>NUL | find /I /C "myapp2.exe"
if not a==b do (
stuff
)
If you don't want to write a file, replace the tasklist and set var1 commands with
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "standard.exe"') do set var1=%%a
same for the second ones.
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "basic.exe"') do set var2=%%a
There is probably a neater way to do it, but the following code seems to do the trick:
:begin
tasklist | find /I /C "standard.exe">D:\tmpfile1.txt
tasklist | find /I /C "basic.exe">D:\tmpfile2.txt
set /p var1= <D:\tmpfile1.txt
set /p var2= <D:\tmpfile2.txt
if %var1% LSS %var2% goto restart
if %var1% EQU %var2% goto wait
:wait
echo waiting..
ping -n 300 127.0.0.1 > nul
goto begin
:restart
echo error has occured, all processes will be restarted
taskkill /f /im standard.exe
taskkill /f /im basic.exe
ping -n 30 127.0.0.1 > nul
goto begin
Cheers!