By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

getting a process's PID

P: n/a
Hello,

I am trying to get python to give me the PID of a process (in this case
HUB). I have it working, except for the fact that the output includes
\012 (newline). Is there a way to ask python not to give me a newline?

Python 1.4 (Oct 14 1997) [C]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>import os
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']

Thanks in advanced for any guidance.

--
Randomly generated signature
Whoever said nothing is impossible never tried slamming a revolving door.
Dec 27 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"eldorado" <el******@io.comwrote in message
news:20*******************@eris.io.com...
Hello,

I am trying to get python to give me the PID of a process (in this case
HUB). I have it working, except for the fact that the output includes
\012 (newline). Is there a way to ask python not to give me a newline?

Python 1.4 (Oct 14 1997) [C]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>import os
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']

There's more than one way to do it! (Oh, sorry, that's Perl...)

The two most standard ways would be to call strip() on your string to get
one sans both leading and trialing whitespace

print h.strip()

or if you know exactly what you've got (i.e., the newline you don't want is
just the last character), you can just get rid of it:

h = h[:-1]
HTH,
-ej
Dec 27 '06 #2

P: n/a
"Erik Johnson" <ej at wellkeeper dot comwrote:
There's more than one way to do it! (Oh, sorry, that's Perl...)

The two most standard ways would be to call strip() on your string to
get one sans both leading and trialing whitespace

print h.strip()

or if you know exactly what you've got (i.e., the newline you don't
want is just the last character), you can just get rid of it:

h = h[:-1]
Or if you don't know for sure it's there, but don't want to lose other
whitespace:

print h.rstrip('\n')

Dec 27 '06 #3

P: n/a
On Wed, 27 Dec 2006, Erik Johnson wrote:
"eldorado" <el******@io.comwrote in message
news:20*******************@eris.io.com...
>Hello,

I am trying to get python to give me the PID of a process (in this case
HUB). I have it working, except for the fact that the output includes
\012 (newline). Is there a way to ask python not to give me a newline?

Python 1.4 (Oct 14 1997) [C]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>>import os
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']


There's more than one way to do it! (Oh, sorry, that's Perl...)

The two most standard ways would be to call strip() on your string to get
one sans both leading and trialing whitespace

print h.strip()

or if you know exactly what you've got (i.e., the newline you don't want is
just the last character), you can just get rid of it:

h = h[:-1]
Thanks for the help, however it doesnt look like those two solutions quite
work:
>>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']
>>h = h[:-1]
h
[]
>>>
>>import string
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
print h.strip()
File "<stdin>", line 1
print h.strip()
^
SyntaxError: invalid syntax

I looked up the syntax for print and it looks correct (at least to me ;)
--
Randomly generated signature
You can go anywhere you want if you look serious and carry a clipboard.
Dec 27 '06 #4

P: n/a

"eldorado" <el******@io.comwrote in message
news:20*******************@eris.io.com...
>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']
>h = h[:-1]
h
[]
Oh, sorry... h is a list here because you are using readlines().
I am used to doing this:

fd = os.popen('ps -ef | grep python')
s = fd.read()

to get a single string. You can do something like

s = h[0]

and then operate on s, or you can use read() in place of readlines() to get
h as a single string, or you can operate on the first element of h:
>>h = ['87334\012']
h[0][:-1]
'87334'
>>h[0].rstrip('\n')
'87334'

All the error handling to do in the case where you actually have multiple
processes being matched is up to you. ;)

-ej
Dec 27 '06 #5

P: n/a
On Wed, 27 Dec 2006, Erik Johnson wrote:
>
"eldorado" <el******@io.comwrote in message
news:20*******************@eris.io.com...
>>>>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']
>>>>h = h[:-1]
h
[]

Oh, sorry... h is a list here because you are using readlines().
I am used to doing this:

fd = os.popen('ps -ef | grep python')
s = fd.read()

to get a single string. You can do something like

s = h[0]

and then operate on s, or you can use read() in place of readlines() to get
h as a single string, or you can operate on the first element of h:
>>>h = ['87334\012']
h[0][:-1]
'87334'
>>>h[0].rstrip('\n')
'87334'

All the error handling to do in the case where you actually have multiple
processes being matched is up to you. ;)
Erik,
Thank you very much. Works perfect. I am now off to work on the multiple
process issue.
--
Randomly generated signature
Claiming that your operating system is the best in the world because more people use it is like saying McDonalds makes the best food in the world.
Dec 27 '06 #6

P: n/a
eldorado <el******@io.comtyped
Hello,

I am trying to get python to give me the PID of a process (in this
case
HUB). I have it working, except for the fact that the output includes
\012 (newline). Is there a way to ask python not to give me a
newline?

Python 1.4 (Oct 14 1997) [C]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>import os
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2
}'") h = g.readlines()
g.close()
h
['87334\012']

Thanks in advanced for any guidance.
Well, you could do everything in python itself, without using grep and
awk at all:
>>>g = os.popen("ps -e -o pid,command")
for line in g.readlines():
if 'HUB' in line:
pid = line.strip().split(' ')[0]
break
print pid
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Dec 27 '06 #7

P: n/a
On Wed, 27 Dec 2006, Sebastian 'lunar' Wiesner wrote:
eldorado <el******@io.comtyped
>Hello,

I am trying to get python to give me the PID of a process (in this
case
HUB). I have it working, except for the fact that the output includes
\012 (newline). Is there a way to ask python not to give me a
newline?

Python 1.4 (Oct 14 1997) [C]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>>import os
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2
}'") h = g.readlines()
g.close()
h
['87334\012']

Thanks in advanced for any guidance.

Well, you could do everything in python itself, without using grep and
awk at all:
>>>>g = os.popen("ps -e -o pid,command")
for line in g.readlines():
if 'HUB' in line:
pid = line.strip().split(' ')[0]
break
print pid
This looks cleaner than the way I was going. I created a file
called ps.py

#!/usr/local/bin/python
import os
g = os.popen("ps -e -o pid,command")
for line in g.readlines():
if 'HUB' in line:
pid = line.strip().split(' ')[0]
break
print pid

When I run ps.py I get the following error.

Traceback (innermost last):
File "./ps.py", line 5, in ?
if 'HUB' in line:
TypeError: string member test needs char left operand

I googled this error, but wasn't smart enough to figure out exactly what
it means.

--
Randomly generated signature
On the other hand, the early worm gets eaten.
Dec 27 '06 #8

P: n/a
eldorado <el******@io.comtyped

[snip]
This looks cleaner than the way I was going. I created a file
called ps.py

#!/usr/local/bin/python
import os
g = os.popen("ps -e -o pid,command")
for line in g.readlines():
if 'HUB' in line:
pid = line.strip().split(' ')[0]
break
print pid

When I run ps.py I get the following error.

Traceback (innermost last):
File "./ps.py", line 5, in ?
if 'HUB' in line:
TypeError: string member test needs char left operand
Strange!? On my system with Python 2.4 I don't get this error. It is
likely to be a problem of your really ancient python version. Do I
guess correctly from your previous postings, that you're still using
version 1.4?.

The only thing, you could advice you to, is to replace the line with the
following code, which should do very much the same thing:
>>>if line.find('HUB') -1:
If you are really using version 1.4, you should replace the next line,
too, because string methods came after 1.4:
>>>pid = string.split(string.strip(line), ' ')[0]
Bye
Sebastian Wiesner

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Dec 27 '06 #9

P: n/a
On Wed, 27 Dec 2006, Sebastian 'lunar' Wiesner wrote:
eldorado <el******@io.comtyped

Strange!? On my system with Python 2.4 I don't get this error. It is
likely to be a problem of your really ancient python version. Do I
guess correctly from your previous postings, that you're still using
version 1.4?.
Sebastian,
Yes, I was running this on a box that had 1.4 - I just tested it on a box
that has 2.3.5 and it runs perfect. Your changes also allow it to be run
on the boxes that still have 1.4
Thanks for all your help.

--
Randomly generated signature
"I have opinions of my own -- strong opinions --but I don't always agree with them."-G.W.Bush
Dec 27 '06 #10

P: n/a
At Wednesday 27/12/2006 17:33, eldorado wrote:
>Yes, I was running this on a box that had 1.4 - I just tested it on a box
that has 2.3.5 and it runs perfect. Your changes also allow it to be run
on the boxes that still have 1.4
Ouch! 1.4 is really really ancient! Even the most conservative
libraries -like PIL- require at least 1.5
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 27 '06 #11

P: n/a
eldorado wrote:
>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']
>h = h[:-1]
h
[]
>>
I understand you're probably set, but instead of using readlines() you
could also do this:

g = os....
h = g.read().split('\n')

and then your 'h' list would not have newlines.

-tom!

--
Dec 28 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.