PDA

View Full Version : [python] is process running, or not?



lazka
January 7th, 2007, 06:41 PM
how do i get the information if e.g. apache is running?

atm i do this:


def check_apache( *args ):
process = os.popen("ps x | grep apache").read().splitlines()
if len(process) > 2:
return 1
else:
return 0

2. question: how do I get the path of the python script itself?
i always have to start it in the folder to get pictures

gpolo
January 7th, 2007, 07:59 PM
answer to question 2: what you want is the current working directory:

import os

print os.getcwd()

answer to question 1: there are lots of ways, do what works for you

lazka
January 7th, 2007, 09:00 PM
sry but thats not what i wanted
it always returns my home-directory path

Tomosaur
January 7th, 2007, 09:05 PM
current directory checking only works on where the user currently is, not the python script. You probably need to set an environmental variable to tell the script where it is.

gpolo
January 7th, 2007, 09:11 PM
I'm sorry but I can't understand then, you arent asking a python question but a shell question ?

M7S
January 8th, 2007, 02:08 AM
What about

os.path.abspath(os.path.dirname(sys.argv[0]))

ghostdog74
January 8th, 2007, 03:04 AM
how do i get the information if e.g. apache is running?

atm i do this:


def check_apache( *args ):
process = os.popen("ps x | grep apache").read().splitlines()
if len(process) > 2:
return 1
else:
return 0

2. question: how do I get the path of the python script itself?
i always have to start it in the folder to get pictures


1.just one way :


process = os.popen("ps x -o pid,args | grep apache").read() #sometimes have to use grep -v grep
if process:
print "apache running"