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? ^
Related
I am using tasklist to bring me information about a specific service/proccess running on my Windows Server.
The command:
tasklist /svc /fi "SERVICES eq .Service02"
The output:
Image Name PID Services
================== ======== ============================================
app02.exe 15668 .Service02
I searched for quite a while now here on StackOverflow, other forums and also on Windows Docs but I couldn't figure out how to get the desired output, which is:
15668
I managed to do a command that kind of worked but not really...
for /f "tokens=1,2 delims= " %A in ('tasklist /svc /fi "SERVICES eq .Service02"') do echo %B
This did not give me the desired output - Instead, it gave me the following output:
C:\Users\admin>echo Name
Name
C:\Users\admin>echo ========
========
C:\Users\admin>echo 15668
15668
If I could only do something that only echoed the third line. The output would be exactly what I need. The PID.
So, I need a command that brings the name of the proccess being used by the service I provide, and return me only its PID.
Please, can someone help me?
Edit: Thanks to #Squashman I managed to do a new command:
tasklist /svc /fi "SERVICES eq .Service02" /FO csv /NH
"service02.exe","15668",".Service02"
And now the output is:
"service02.exe","15668",".Service02"
But where do I go from here?
Just use a for /F loop to capture the CSV output of the tasklist command and to extract the right token.
In Command Prompt:
#for /F "tokens=2 delims=," %P in ('tasklist /SVC /FI "Services eq .Service02" /FO CSV /NH') do #echo %~P
In a batch file:
#echo off
for /F "tokens=2 delims=," %%P in ('
tasklist /SVC /FI "Services eq .Service02" /FO CSV /NH
') do echo %%~P
The ~-modifier removes the surrounding quotation marks from the PID value.
You could of course retrieve the PID using the Service Control executable, sc.exe instead.
#For /F "Tokens=3" %%G In ('%SystemRoot%\System32\sc.exe QueryEx .Service02 ^| %SystemRoot%\System32\find.exe "PID" 2^>NUL') Do #Set "PID=%%G"
However, based upon your reply in comment, here's a quick example to show you how you may be able to perform the task without any need for retrieving the PID:
#Set "SvcName=.Service02"
#Set "SysDir=%SystemRoot%\System32"
#Rem Stop service if memory usage is greater than or equal to 150 MB
#%SysDir%\tasklist.exe /Fi "Services Eq %SvcName%" /Fi "MemUsage GE 153600" /Fo CSV /NH /Svc | %SysDir%\findstr.exe /I /R ",\"%SvcName%\"$" 1>NUL && (
%SysDir%\sc.exe Stop %SvcName%
Rem Add a delay to give the service time to stop
%SysDir%\timeout.exe /T 5 /NoBreak 1>NUL
Rem If service state is stopped then start service again
%SysDir%\sc.exe Query %SvcName% | %SysDir%\findstr.exe /R /C:"STATE *: 1 " 1>NUL && %SysDir%\sc.exe Start %SvcName%)
Line 7 can be adjusted to increase the timeout period from 5 seconds as needed.
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.
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!