LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2012, 01:17 PM   #1
cymplecy
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Rep: Reputation: Disabled
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
 
Old 07-26-2012, 02:21 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
You can kill the first one and it should reap the child processes along with it.
 
Old 07-31-2012, 11:10 AM   #3
cymplecy
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-01-2012, 11:38 AM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 08-02-2012, 03:42 AM   #5
cymplecy
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-02-2012, 05:02 AM   #6
jsaravana87
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 558
Blog Entries: 5

Rep: Reputation: Disabled
you Can try these


Quote:
ps aux | grep python | grep -v grep | awk '{ print $2 }' | xargs kill -9
 
1 members found this post helpful.
Old 08-02-2012, 11:31 AM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by arun5002 View Post
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.
 
1 members found this post helpful.
Old 08-02-2012, 03:56 PM   #8
cymplecy
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 08-03-2012, 01:53 PM   #9
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kill a running process carlosOFF Linux - Server 4 07-19-2011 11:37 AM
help w/ script to kill process(es) based on time they've been running. jcardoza Linux - Newbie 4 12-09-2010 11:08 AM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 11:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration