473,320 Members | 2,124 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,320 software developers and data experts.

Processes in Linux from Python

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.
Sep 1 '08 #1
4 1541
Johny schrieb:
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
Sep 1 '08 #2
Johny wrote:
Is it possible to get a number of the http processes running on Linux
directly from Python ?
The Python core has no direct API for the job. However you can use the
virtual /proc/ file system for the job. Or check out my enumprocess
package.
http://pypi.python.org/pypi?:action=...ss&version=0.1
It's far from perfect but it should do the job on Linux.

Christian

Sep 1 '08 #3
On Sun, 31 Aug 2008 23:25:56 -0700 (PDT), Johny wrote:
To get a number of the http processes running on my Linux( Debia box)
I use
ps -ef | grep "[h]ttpd" | wc -l
....
So my question is:
Is it possible to get a number of the http processes running on Linux
directly from Python ?
Yes, it is. There is a number of third party packages that provide
such functionality, including PSI: http://www.psychofx.com/psi/
I never actually liked them, because they are parsing /proc directory
by themselves. Thus I wrote my own tool that is a wrapper around
procps library (libproc* in your /lib, probably). Your system tools
like ps, w or top are using this library. My wrapping library is
available at: http://code.google.com/p/procpy/
In your case you could use it like this:
>>import procpy
pp = procpy.Proc()
for pid in pp.pids:
.... if pp.procs[pid]['cmd'] == 'apache2':
.... print pp.procs[pid]['tid']
....
5204
5205
5206
5208
>>>
it prints the PIDs of all the apache2 processes on my system.
Procpy is fine for my own needs, but if I were about to write
a code that is intended to be portable, I'd use PSI. It also is
more mature.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 1 '08 #4
On Mon, Sep 01, 2008 at 08:40:42AM +0200, Diez B. Roggisch wrote:
Johny schrieb:
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+EikybHt vIk+r/wACeOVIq
eFRLkEJgzX3W9kGnWVnAP5A=
=9ZfI
-----END PGP SIGNATURE-----

Sep 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guy | last post by:
Hi I've done some projects in python for my work which invole some test scripts etc. All quite basic stuff, I find python a good language to work in but have come up against a brick wall. I'm...
2
by: Markus Franz | last post by:
Hi. Today I created a script called load.py for using at the command line written in Python 2.3. This script should load as many websites as given on the comand line and print them with a...
6
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
0
by: David H | last post by:
Background. I'm running on WinXP w/ MS Services for Unix installed (to give rsh/rlogin ability), both Python 2.3 and 2.4 version. In linux, I'm running RHEE with python2.3 version. The code...
1
by: Sori Schwimmer | last post by:
Hi, I am working on an application which involves interprocess communication. More to the point, processes should be able to notify other processes about certain situations, so the "notifyees"...
4
by: Robin Haswell | last post by:
Hey there I'm doing some threading in python with Python 2.3 and 2.4 on Ubuntu and Debian machines, and I've noticed that if I open a lot of threads (say, 50), I get lots of python processes...
13
by: Godzilla | last post by:
Hello, How do you create/spawn new processes in XP over telnet using python? I.e. I would like to create a new process and have it running in the background... when I terminate the telnet...
1
by: Falcolas | last post by:
Hi all, This may be more of a Linux question, but it relates to how Python forks... Today, I implemented a pretty simple listener script using os.fork. The script runs fine, and performs as...
0
by: Neal Becker | last post by:
Hi numeric processing fans. I'm pleased to report that you can now have convenient checkpoint/restart, at least if you are running fedora linux. Example: python -i blcr_mod.py <<< this will...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.