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

H

herman

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.
 
F

Furkan KURU

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)
 
M

Michael Bentley

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
 
M

Michael Bentley

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
 
S

Steven D'Aprano

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.
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.
 
K

Karthik Gurusamy

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.

Your script can then use the output as its input.

Karthik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top