LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Kill process(es) of a running script (https://www.linuxquestions.org/questions/linux-newbie-8/kill-process-es-of-a-running-script-4175418820/)

cymplecy 07-26-2012 01:17 PM

Kill process(es) of a running script
 
I've created a script
Code:

#!/bin/bash
sudo python scratch_gpio_handler.py

that runs a python program to handle traffic between Scratch and the GPIO pins on a RaspberryPi.

This program consumes a lot of CPU (as its just sits in a loop awaiting changes) and I'd like to kill it when I finished.

Looking at ps aux I've got these 3 lines

/bin/bash ./sgh (the name of the script file)
sudo python scratch_gpio_handler.py
python scratch_gpio_handler.py

My question is do I have to kill all three or can I get away with just killing the last one?

regards
Simon

Kustom42 07-26-2012 02:21 PM

You can kill the first one and it should reap the child processes along with it.

cymplecy 07-31-2012 11:10 AM

Killing the first one only doesn't seem to work for me - the others seem to stay running.

Killing the last one seems to remove all of them but I can't work out the syntax to supply to pkill

Code:

sudo pkill python
works but I wouldn't want to use it in case there are other python programs running.

Any suggestions please?

regards
Simon

Kustom42 08-01-2012 11:38 AM

You will need to find the parent process and kill that, it will then reap the child processes. The .sh process is usually the parent process.

A ps aux | grep python will give you the process numbers as well as the PPID(parent process ID). You can then check that PPID and kill it.

cymplecy 08-02-2012 03:42 AM

As I said, killing the bin/bash process doesn't seem to kill the other two for me.

But killing the python scratch_gpio_handler.py process does seem to kill all the others as well.

But my problem now is that I can't work out the syntax to kill that one :(


Simon

jsaravana87 08-02-2012 05:02 AM

you Can try these


Quote:

ps aux | grep python | grep -v grep | awk '{ print $2 }' | xargs kill -9

Kustom42 08-02-2012 11:31 AM

Quote:

Originally Posted by arun5002 (Post 4743933)
you Can try these

Be careful with that, we should explain that this will send a kill signal of 9 to any running process that has python in the name. Remove the xargs portion and run the command to see what it will kill before actually executing it. Just a heads up.

cymplecy 08-02-2012 03:56 PM

Code:

sudo ps aux | grep "python scratch_gpio_handler.py" | grep -v grep | awk '{print $2}' | xargs sudo kill -9
Seems to do the job :)

Thanks for the help people :)

Simon

Kustom42 08-03-2012 01:53 PM

Good code cymplecy, you are limiting your grep to a specific process that way instead of killing all python processes. The one killing all python could do something like kill yum during a transaction and bork your rpmdb as well as yum.


All times are GMT -5. The time now is 05:39 PM.