473,386 Members | 1,812 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,386 software developers and data experts.

getting a process's PID

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
11 6560
"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
"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
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

"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fei Yuan | last post by:
Please forgive me re-posting this question since I wasn't clear in my original post. ---- Starting an external process needs to pass it a ProcessStartInfo() object. ProcessStartInfo has a...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
2
by: Henrik | last post by:
Hi all, I'm working with a win app in vb that needs access to actual process id of the office-applications Word, Excel and PowerPoint. I know I can loop the processes and check for processes of...
2
by: py | last post by:
Is there a way in python to figure out which process is running on which port? I know in Windows XP you can run "netstat -o" and see the process ID for each open port....but I am looking for...
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
4
by: eva.monsen | last post by:
I'm trying to run a .BAT file using System.Diagnostics.Process. I'm having trouble getting Process.ExitCode to match up with what the .BAT file returns. Here are the contents of...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
4
shoonya
by: shoonya | last post by:
i am using windows XP and using popen("start notepad","r"); to start a note pad now how can i get the process id of this notepad shoonya
1
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h...
6
by: Ashutosh Bhawasinka | last post by:
Hi, I am starting a process and I need to monitor it (wait and check if its still responding). But, I it seems that the Process.HasExited or Process.Exited doesn't work. I just need to know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.