find pid for processes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

find pid for processes

#1 Post by r2du-soft » 15 Mar 2012 10:06

hi
I need quick help for projects
I need a command that find Pid processes and As a variable which.
for example
find pid processes "egui" and = abc
and
I love it I use the following command

taskkill /pid %abc% /f

Because I find it And define it as a variable that pid After each time restart Change

Is it possible to do?

very thanks

aGerman
Expert
Posts: 4742
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find pid for processes

#2 Post by aGerman » 15 Mar 2012 18:33

Have a look at the TASKLIST command. But why are you not simply using the image name (/im process.exe)? Why the indirect way via PID?

Regards
aGerman

r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

Re: find pid for processes

#3 Post by r2du-soft » 16 Mar 2012 03:26

Because that is antivirus (nod32) process And is very strong ant not kill with /im process.
that process just kill with /pid
I want to command A process pid As variables to consider
For example
pid process egui.exe = abc
abc aVariable
؟؟؟

So thanks

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: find pid for processes

#4 Post by abc0502 » 16 Mar 2012 10:09

why closing the nod32 just remove it if you don't need it...?
but any way
see this:
http://www.dostips.com/forum/viewtopic.php?f=3&t=1016&start=0

and u can change the code to this :

Code: Select all

@echo off &setlocal

set "process=firefox.exe"

for /f "tokens=2" %%i in ('tasklist /nh /fi "imagename eq %process%" 2^>nul') do set PID=%%i
taskkill /F /pid %PID%


but i dout u can kill antivirus process because they protect their process.

I didn't try.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: find pid for processes

#5 Post by Ed Dyreen » 16 Mar 2012 10:56

'
disabling the NOD service is a little more safer/friendly, see net start and net stop commands net /? and services.msc

r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

Re: find pid for processes

#6 Post by r2du-soft » 16 Mar 2012 12:47

wow :shock:
Thank you very much
God give you everything you want
very Thanks
+
up command (code) find pid process and set As Variable
Now
for find pid services and set As Variable in the under code

Code: Select all

@echo off &setlocal

set "process=firefox.exe"

for /f "tokens=2" %%i in ('tasklist /nh /fi "imagename eq %process%" 2^>nul') do set PID=%%i
taskkill /F /pid %PID%

what Should Replace (process and %process%)
im Replace (sc & net) but program Did not work properly!!!

what Should Replace (process and %process%)؟

SO Thanks

aGerman
Expert
Posts: 4742
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find pid for processes

#7 Post by aGerman » 16 Mar 2012 16:30

Because you told us you want to kill the egui.exe process you should change:

Code: Select all

set "process=egui.exe"

You could also append a PAUSE command to the code to see whether an error occurs.

Regards
aGerman

r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

Re: find pid for processes

#8 Post by r2du-soft » 16 Mar 2012 17:03

Is true،im underestand.
Excellent

Code: Select all

set "process=egui.exe"

I Except up code ,i a new code want till Instead process pid , find service pid!!
Instead "process=service" What code do؟

aGerman
Expert
Posts: 4742
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find pid for processes

#9 Post by aGerman » 16 Mar 2012 17:29

What are you doing with the PID of a service? You cannot apply TASKKILL on services. You have to use NET STOP instead as Ed already suggested.

Regards
aGerman

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: find pid for processes

#10 Post by abc0502 » 18 Mar 2012 06:55

hi,
I don't know how to get pid of a service and not all services has a pid number
stopping service with one of two command sc or net
you can stop services as a German and Ed Dyreen said

Code: Select all

net stop servicename

or

Code: Select all

use sc stop servicename


these two command dosn't require pid number.
you will need administrator privilage to use these commands if using windows 7

aGerman
Expert
Posts: 4742
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find pid for processes

#11 Post by aGerman » 18 Mar 2012 12:07

abc0502 wrote:I don't know how to get pid of a service

Code: Select all

set "service=Schedule"
for /f "skip=1 delims=" %%i in ('wmic service where name^="%service%" get ProcessID') do set /a PID = %%i
echo %PID%

... but I don't know what this information could be good for :wink:

Regards
aGerman

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: find pid for processes

#12 Post by abc0502 » 19 Mar 2012 08:34

@aGerman
I used the code but it come up with this line before showing the pid number

Code: Select all

Missing operand.
976


do you know why? thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: find pid for processes

#13 Post by foxidrive » 19 Mar 2012 09:11

With this I get the following. I'm not sure why either. I certainly have the Workstation service running.

Code: Select all

@echo off
set "service=Schedule"
set "service=Workstation"

for /f "skip=1 delims=" %%i in ('wmic service where name^="%service%" get ProcessID') do set /a PID = %%i
echo %PID%
pause


No Instance(s) Available.

aGerman
Expert
Posts: 4742
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find pid for processes

#14 Post by aGerman » 19 Mar 2012 11:13

@abc0502
As I had to learn the output of different wmic versions have differently formatted character encodings. Sometimes it results in strange line breaks (like <CR><CR><LF>). The error you see is probably caused by such a control character that SET /A cannot process. The simpliest way is to redirect the message to NUL.

Code: Select all

for /f "skip=1 delims=" %%i in ('wmic service where name^="%service%" get ProcessID') do set /a PID = %%i 2>nul


@foxidrive
Not each service runs with a PID. Have a look at the services-tab in your taskmanager before.

Regards
aGerman

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: find pid for processes

#15 Post by abc0502 » 20 Mar 2012 00:01

it work fine now
thanks :)

Post Reply