Processes in Linux from Python

J

Johny

To get a number of the http processes running on my Linux( Debia box)
I use
ps -ef | grep "[h]ttpd" | wc -l

But If I want to use to get a number of the http processes from my
Python program I must use a popen command e.g.
popen2.popen3('ps -ef | grep "[h]ttpd" | wc -l')
that is I must call an external command from Python.

But it creates a zombie.
So my question is:
Is it possible to get a number of the http processes running on Linux
directly from Python ?

Thanks for help.
L.
 
D

Diez B. Roggisch

Johny said:
To get a number of the http processes running on my Linux( Debia box)
I use
ps -ef | grep "[h]ttpd" | wc -l

But If I want to use to get a number of the http processes from my
Python program I must use a popen command e.g.
popen2.popen3('ps -ef | grep "[h]ttpd" | wc -l')
that is I must call an external command from Python.

The shell does the exact same thing. And by the way: i think you miss a

grep -v grep

after the first grep - otherwise you count the grep itself.
But it creates a zombie.

Try using the subprocess module. It should not require you to call wait
yourself.
So my question is:
Is it possible to get a number of the http processes running on Linux
directly from Python ?

Not really, or at least not with less effort & without duplicating
system tools functionality.

Diez
 
D

Derek Martin

Johny said:
To get a number of the http processes running on my Linux( Debia box)
I use
ps -ef | grep "[h]ttpd" | wc -l
[...]
The shell does the exact same thing. And by the way: i think you miss a

grep -v grep

Indeed not. The brackets around the 'h' (which make it a character
class, or range if you prefer) prevent the regex from matching itself.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIvK1ydjdlQoHP510RAmDHAJ4yxEcKkE3zOd+EikybHtvIk+r/wACeOVIq
eFRLkEJgzX3W9kGnWVnAP5A=
=9ZfI
-----END PGP SIGNATURE-----
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top