Raul Chiarella
Raul Chiarella

Reputation: 712

Get PID from tasklist command

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?

Upvotes: 1

Views: 3181

Answers (2)

Compo
Compo

Reputation: 38623

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.

Upvotes: 1

aschipfl
aschipfl

Reputation: 34909

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.

Upvotes: 3

Related Questions