getting a process's PID

E

eldorado

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['87334\012']

Thanks in advanced for any guidance.
 
E

Erik Johnson

eldorado said:
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['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
 
D

Duncan Booth

Erik Johnson said:
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')
 
E

eldorado

eldorado said:
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 []
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 ;)
 
E

Erik Johnson

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
 
E

eldorado

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

Sebastian 'lunar' Wiesner

eldorado said:
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['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
 
E

eldorado

eldorado said:
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.
 
S

Sebastian 'lunar' Wiesner

eldorado <[email protected]> typed

[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 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
 
E

eldorado

eldorado <[email protected]> typed

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
 
G

Gabriel Genellina

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
 
T

Tom Plunket

eldorado said:
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!

--
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top