PDA

View Full Version : How to detect a running process and kill it using Python?


echarcha
May 8th, 2007, 09:58 AM
I think our resident python guru GunsNRoses can answer this.

The requirements are -
Python (latest version) is running on a Ubuntu Linux box.

There is a process running (whose name is known. i.e. you can see the name 'fooprocess' when top command is run).

1) The python script should check if this process 'fooprocess' is running.
2) If it is, then 'fooprocess' should be terminated.
3) Then the script will do something else
4) Finally the script will run the process 'fooprocess' again. i.e. launch it fresh.

My knowledge of python is very very limited and I could only get to the detection of the /proc directory.

vyomkeshsaxena
May 8th, 2007, 10:16 AM
I think our resident python guru GunsNRoses can answer this.

dint know that GnR pai was a snake charmer
The requirements are -
Python (latest version) is running on a Ubuntu Linux box.

damnnn....and u r watching?? :eek: call someone from zoo to catch him unless u r directing a filumm

There is a process running (whose name is known. i.e. you can see the name 'fooprocess' when top command is run).

1) The python script should check if this process 'fooprocess' is running.

i heard snakes have really good sensory perception and they use smell and ground vibrations to detct the prey..so dont worry..if sth is running..python will check it

2) If it is, then 'fooprocess' should be terminated.

ek baar python ki pakad mein aa gaya to terminate to hona hi hain..so dont worry
3) Then the script will do something else

are u producing filum with python in it???

4) Finally the script will run the process 'fooprocess' again. i.e. launch it fresh.

wow...aur ek chase sequence..amazing script kharchu :up:
My knowledge of python is very very limited and I could only get to the detection of the /proc directory.

ohh my advice..dont play wid these creatures...they are very deadly...not much poisonous but kills their prey by strangulating them...get professionals to do the job for u...dont go near it


hope the solution was of some help...:)

GunsNRoses
May 8th, 2007, 10:44 AM
#!/usr/bin/env python
import os
import signal

# Change this to your process name
processname = 'aterm'

for line in os.popen("ps xa"):
fields = line.split()
pid = fields[0]
process = fields[4]

if process.find(processname) > 0:
# Kill the Process. Change signal.SIGHUP to signal.SIGKILL if you like
os.kill(int(pid), signal.SIGHUP)

# Do something else here
print "Doing something else here"

# Restart the process
os.system(processname)

# Hop out of loop
break


There's also the good ol' killall command you can use with os.system()

echarcha
May 8th, 2007, 01:48 PM
#!/usr/bin/env python
import os
import signal

# Change this to your process name
processname = 'aterm'

for line in os.popen("ps xa"):
fields = line.split()
pid = fields[0]
process = fields[4]

if process.find(processname) > 0:
# Kill the Process. Change signal.SIGHUP to signal.SIGKILL if you like
os.kill(int(pid), signal.SIGHUP)

# Do something else here
print "Doing something else here"

# Restart the process
os.system(processname)

# Hop out of loop
break


There's also the good ol' killall command you can use with os.system()

Thank you very much.. This is good start..

HarHarMahaDev
May 8th, 2007, 02:27 PM
#!/usr/bin/env python
import os
import signal

# Change this to your process name
processname = 'aterm'

for line in os.popen("ps xa"):
fields = line.split()
pid = fields[0]
process = fields[4]

if process.find(processname) > 0:
# Kill the Process. Change signal.SIGHUP to signal.SIGKILL if you like
os.kill(int(pid), signal.SIGHUP)

# Do something else here
print "Doing something else here"

# Restart the process
os.system(processname)

# Hop out of loop
break


There's also the good ol' killall command you can use with os.system()


Now can u please tell us how to kill the process using rattlesnake?

aragorn
May 8th, 2007, 07:30 PM
Now can u please tell us how to kill the process using rattlesnake?


rattlesnake is not compatible with current breed of OS. It will run on Snake Charmer OS. Which is underdevelopment. You will have to wait for its release after that you will be able to use rattlesnake. until them feed him milk :D

Soorma Bhopali
May 8th, 2007, 10:28 PM
hope the solution was of some help...:)

Topic dekhkar hi mein samajh gaya tha ki koi-n-koi iski vaat lagayega

Top-class solution from Vyomkesh pai.

:rotfl: :rotfl: :rotfl: :rotfl:

vyomkeshsaxena
May 8th, 2007, 11:28 PM
Now can u please tell us how to kill the process using rattlesnake?

yeah...itz easy...syntax is same for rattlesnake...dont change the process name in the script....it will give an error...dont fix it by giving an antidote...process/project killed!!

Napolean
May 8th, 2007, 11:34 PM
Well,my 2 cents to general public :-
Be very careful while dealing with snakes,because if it bites you and thereby kills you,you have lost very important part of your life.

vyomkeshsaxena
May 9th, 2007, 12:16 AM
Well,my 2 cents to general public :-
Be very careful while dealing with snakes,because if it bites you and thereby kills you,you have lost very important part of your life.

hehhe nappy just rephrased a very famous quotation by groucho marx or al gore i guess ...copycatttt :p

aragorn
May 9th, 2007, 06:12 AM
Well,my 2 cents to general public :-
Be very careful while dealing with snakes,because if it bites you and thereby kills you,you have lost very important part of your life.


yup....i too want to say to general public stop messing with cha pai's thread..otherwise his one eyed snake will bite u and might spit on you :D