Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how can I find out the process ids with a process name

Reply
Thread Tools

how can I find out the process ids with a process name

 
 
herman
Guest
Posts: n/a
 
      09-02-2007
Hi,

I would like to find out all the process id with the process name
'emacs'.

In the shell, i can do this:

$ ps -ef |grep emacs
root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
stressTestVideo.py
root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
gtk
root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs


and I can see the process id is 20731, 25649, etc, etc.

But now I would like to do the programmically in my python script.
I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
grep emacs', but how
can I pipe the output of my 'ps -ef | grep emacs' to my python script
and then run a regression expression with it to get the process Ids?

Thank you.

 
Reply With Quote
 
 
 
 
Furkan KURU
Guest
Posts: n/a
 
      09-02-2007
the easiest but slowest way:

you can send output to a file

ps -ef |grep emacs > output_file

and then read the file content

(I believe there is a much better way)




On 9/2/07, herman <(E-Mail Removed)> wrote:
> Hi,
>
> I would like to find out all the process id with the process name
> 'emacs'.
>
> In the shell, i can do this:
>
> $ ps -ef |grep emacs
> root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
> root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
> root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
> stressTestVideo.py
> root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
> gtk
> root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs
>
>
> and I can see the process id is 20731, 25649, etc, etc.
>
> But now I would like to do the programmically in my python script.
> I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
> grep emacs', but how
> can I pipe the output of my 'ps -ef | grep emacs' to my python script
> and then run a regression expression with it to get the process Ids?
>
> Thank you.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Furkan Kuru
 
Reply With Quote
 
 
 
 
Michael Bentley
Guest
Posts: n/a
 
      09-02-2007

On Sep 2, 2007, at 12:26 PM, herman wrote:

> I would like to find out all the process id with the process name
> 'emacs'.
>
> In the shell, i can do this:
>
> $ ps -ef |grep emacs
> root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
> root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
> root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
> stressTestVideo.py
> root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
> gtk
> root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs
>
>
> and I can see the process id is 20731, 25649, etc, etc.
>
> But now I would like to do the programmically in my python script.
> I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
> grep emacs', but how
> can I pipe the output of my 'ps -ef | grep emacs' to my python script
> and then run a regression expression with it to get the process Ids?


Are you targeting Linux? If so, have a look at the /proc system.
Each process has a directory, and the 'status' file in each process'
directory tells many things, including process name (the line that
ends with the process name, begins with 'Name').

Here's a quick bashy way to get pid + process names:

cd /proc
for i in ls [0-9]*/status
do
echo $i `grep '^Name' $i | cut -f2` | sed 's/\/status//g'
done


hth,
Michael
---
"If we had asked people what they wanted they would have said 'a
faster horse'." --Henry Ford


 
Reply With Quote
 
Michael Bentley
Guest
Posts: n/a
 
      09-02-2007
>
> cd /proc
> for i in ls [0-9]*/status
> do
> echo $i `grep '^Name' $i | cut -f2` | sed 's/\/status//g'
> done
>



Um...

cd /proc
for i in `ls [0-9]*/status`
do
echo $i `grep '^Name' $i | cut -f2` | sed 's/\/status//g'
done


---
Let the wookie win.



 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-02-2007
On Sun, 02 Sep 2007 19:26:27 +0000, herman wrote:

> But now I would like to do the programmically in my python script. I
> know I can use ' os.system(cmd)' to execute the command 'ps -ef | grep
> emacs', but how
> can I pipe the output of my 'ps -ef | grep emacs' to my python script
> and then run a regression expression with it to get the process Ids?


Use popen.

>>> f = os.popen('ps ax | grep -i PYTHON')
>>> print f.read()

1952 ? Ssl 0:01 /usr/bin/python -E /usr/sbin/setroubleshootd
2117 ? S 0:00 python ./hpssd.py
2376 ? SN 3:19 /usr/bin/python /usr/sbin/yum-updatesd
18087 pts/4 S+ 0:00 python
18115 pts/4 S+ 0:00 sh -c ps ax | grep -i PYTHON
18117 pts/4 R+ 0:00 grep -i python


There is also a module popen2 which does similar but more advanced things.



--
Steven.
 
Reply With Quote
 
Karthik Gurusamy
Guest
Posts: n/a
 
      09-04-2007
On Sep 2, 12:26 pm, herman <(E-Mail Removed)> wrote:
> Hi,
>
> I would like to find out all the process id with the process name
> 'emacs'.
>
> In the shell, i can do this:
>
> $ ps -ef |grep emacs
> root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
> root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
> root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
> stressTestVideo.py
> root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
> gtk
> root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs
>
> and I can see the process id is 20731, 25649, etc, etc.
>
> But now I would like to do the programmically in my python script.
> I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
> grep emacs', but how
> can I pipe the output of my 'ps -ef | grep emacs' to my python script
> and then run a regression expression with it to get the process Ids?
>


Try commands module; it's simple to just get the output. subprocess
module is a newer way to doing things. But commands.getoutput() is lot
simpler for simple shell like tasks.

>>> import commands
>>> commands.getoutput("ps -ef | grep emacs | awk '{print $2}'")

'21739\n15937\n15287\n5097\n14797\n31777\n8779\n29 73\n5413\n13024\n13026'
>>>


Your script can then use the output as its input.

Karthik

> Thank you.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how can i retrieve the name of form when one of the element name in this form is 'name' ashok.dhananjeyan@gmail.com Javascript 6 11-24-2006 02:54 AM
XSLT - How can I find out the name of an Element Lenny XML 2 07-14-2005 09:57 AM
after transfer of data from MS-outlook(mail ids) to application,mail ids are consisting of strange characters vamsikrishna_b@coolgoose.com Python 2 06-21-2005 12:41 PM
view process ids without having to go into COM+ (Component Services) TJ ASP General 1 10-03-2003 09:09 PM



Advertisments