Python [snippet/problem] : Kill process via python, script won’t continue

Have You wondering, how to kill process in Your linux machine and restart it again? I have some problem with it, let’s see the first code now.

#usr/bin/python
import os, time

sleepTime = 3

''' STEP 1 - Kill Stream '''
text = 'TwitterStreamEncode'
#execute = 'kill -9 `ps -ef | grep %s | grep -v grep | awk \'{print $2}\'`' % sys.argv[1] # ==> Note 1
execute = 'kill -9 `ps -ef | grep %s | grep -v grep | awk \'{print $2}\'`' % text # ==> Note 2
print execute
cmdKill  = os.system(execute)

''' STEP 2 - Give sleep to make sure process is killed '''
print "Sleeping for %s sec\n" % sleepTime
time.sleep(sleepTime)

Here’s the command to start my test

root@fajri-laptop : ~# nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null &
[1] 28435
root@fajri-laptop : ~# nohup: ignoring input and redirecting stderr to stdout
^C
root@fajri-laptop : ~# ps -ef | grep Twitter
root     28435 28424  2 21:13 pts/1    00:00:00 /usr/bin/php /home/project/MW/TwitterStreamEncode.php
root     28438 28424  0 21:13 pts/1    00:00:00 grep --color=auto Twitter

You can see, the process is starting with PID 28435
Then I’m execute it like usual, python try_kill.py

root@vx662-sekremm:/home/project/MW# python try_kill.py
kill -9 `ps -ef | grep TwitterStreamEncode | grep -v grep | awk '{print $2}'`
Sleeping for 3 sec

root@fajri-laptop : ~# ps -ef | grep Twitter
root     28451 28424  0 21:16 pts/1    00:00:00 grep --color=auto Twitter
[1]+  Killed                  nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null

You can see, the first script is fine, the process is killed.

But, I want to make it simpler, myscript able to receive parameter via command line, like python try_kill.py TwitterStreamEncode, so it will find the process, and kill it.
So I change my code to Note 1 #execute = 'kill -9 `ps -ef | grep %s | grep -v grep | awk \'{print $2}\'`' % sys.argv[1] , and here’s the output.

root@fajri-laptop : ~# nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null &
[1] 28473
root@fajri-laptop : ~# nohup: ignoring input and redirecting stderr to stdout
^C

root@fajri-laptop : ~# ps -ef | grep Twitter
root     28473 28424  5 21:20 pts/1    00:00:00 /usr/bin/php /home/project/MW/TwitterStreamEncode.php
root     28476 28424  0 21:20 pts/1    00:00:00 grep --color=auto Twitter

root@vx662-sekremm:/home/project/MW# python try_kill.py TwitterStreamEncode
kill -9 `ps -ef | grep TwitterStreamEncode | grep -v grep | awk '{print $2}'`
Killed

root@fajri-laptop : ~# ps -ef | grep Twitter
root     28485 28424  0 21:22 pts/1    00:00:00 grep --color=auto Twitter
[1]+  Killed                  nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null

You can see, my script won’t continue after it execute os.system
I’m still don’t know why this is happened 😦

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s