473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2135
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.  In each directory, I need to process each image and generate an HTML file listing all of the images and links to the subdirectories....
2
by: Sri | last post by:
I am writing an asp.net applicaition using VB coding. In a function, I am opening an excel file with the following code, Dim objExcel As Object Dim objWorkBook As Object objExcel =...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
1
by: DD | last post by:
I'm not sure that this msg made it out, the first time I sent it, so I am trying again. -- Win XP Home Edition I use System.Diagnostics.Process.GetProcesses()) to get info about the processes...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
2
by: Han de Monnink | last post by:
Hi, I am looking for a way to determine het user that is running a certain process, I can retrieve the process ID by calling the GetProcessByName("test") method which will return all processes...
5
by: su | last post by:
to find which process dumped core at the promt we give $ file core.28424 core.28424: ELF 32-bit LSB core file of 'soffice.bin' (signal 11), Intel 80386, version 1 (SYSV), from 'soffice.bin' ...
2
by: DBC User | last post by:
I have a program which ccan be called multiple times. Each time, it will open a window and the windows title has unique name. I do not have any control over this program. I want to know is there a...
2
by: moondaddy | last post by:
I had to repost this because I had to update and change my msdn alias. I will re-ask the question and clarify a few things that were not clear before. This code is all executed on my dev...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.