471,624 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

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

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.

Sep 2 '07 #1
5 2030
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 <He************@gmail.comwrote:
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
Sep 2 '07 #2

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
Sep 2 '07 #3
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.
Sep 2 '07 #4
>
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.

Sep 3 '07 #5
On Sep 2, 12:26 pm, herman <Herman.Schu...@gmail.comwrote:
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.

Sep 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan Jones | last post: by
2 posts views Thread by Han de Monnink | last post: by
2 posts views Thread by moondaddy | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.