LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 06-14-2013, 02:37 PM   #1
michael.barnes
LQ Newbie
 
Registered: Jan 2007
Location: North Texas
Distribution: SUSE 9, 10; CentOS4,5;Redhat; (K)Ubuntu; Knoppix
Posts: 23

Rep: Reputation: 0
killing multiple processes


I have several scripts running as cron jobs. For some reason, the scripts are not working (that's another issue for later) and they are leaving hundreds of hung processes. I'm trying to kill them as a group by using the following command.
Code:
kill `ps aux | grep rduser | grep scale | awk '{print $2}'`
A typical entry running ps aux looks like this.
Code:
rduser    2767  0.0  0.0   3340   548 ?        SN   Jun02   0:00 lame -m s -a --scale 20 --quiet --nohist /mnt/TV3/010003_022.wav /var/snd/ftp/FNC/FNC_Sun
Since each line I want to kill has the word "scale" in it, this should return a list of processes with the word scale and kill them. The problem is when I run the kill command, it halts with a non-existent process number.

Code:
$ kill `ps aux | grep rduser | grep scale | awk '{print $2}'`
-bash: kill: (29838) - No such process
I can run the command without the kill and it will print out a proper number of processes. But each time I run it with kill, it is like it is getting a different list and failing.

Any ideas on what I am doing wrong would be appreciated.

Michael
 
Old 06-14-2013, 03:04 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,653
Blog Entries: 2

Rep: Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096
The
Code:
grep scale
part of that command will find itself, since it has scale in the command name. When the part in backticks has run (by the way, the $() syntax is better than backticks) and returns all the process numbers the grep process doesn't exist anymore, which causes the kill command to throw that error.
 
Old 06-14-2013, 03:14 PM   #3
michael.barnes
LQ Newbie
 
Registered: Jan 2007
Location: North Texas
Distribution: SUSE 9, 10; CentOS4,5;Redhat; (K)Ubuntu; Knoppix
Posts: 23

Original Poster
Rep: Reputation: 0
I think I understand that. So what do I do to fix it?
 
Old 06-14-2013, 03:32 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,653
Blog Entries: 2

Rep: Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096Reputation: 4096
Add a
Code:
grep -v grep
into the pipe after the grep for scale.
 
Old 06-14-2013, 04:59 PM   #5
michael.barnes
LQ Newbie
 
Registered: Jan 2007
Location: North Texas
Distribution: SUSE 9, 10; CentOS4,5;Redhat; (K)Ubuntu; Knoppix
Posts: 23

Original Poster
Rep: Reputation: 0
That was the secret. Works fine now. Thanks.
 
Old 06-14-2013, 06:59 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,777
Blog Entries: 54

Rep: Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978Reputation: 2978
Argh. 'pgrep' and 'pkill' were meant to combat that. See 'man pkill', the "-f" switch. Example:
Code:
]$ \ps --no-headers -C lame -o args
/usr/bin/lame -m s -a --scale 20 --quiet --nohist /mnt/TV3/010003_022.wav /var/snd/ftp/FNC/FNC_Sun

]$ pgrep lame # matches process name
2767

]$ pkill -9 lame # matches process name

]$ pkill -9 -f "iet --nohist /m"  # matches any part of the process
 
Old 06-17-2013, 01:39 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Note that you can get a similar 'not found' effect if you use a simple kill on progs that fork.
If the child dies, the parent may also exit before your kill cmd gets to it.
 
Old 06-17-2013, 01:53 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Since you are already using awk, get it to do the kill

Code:
ps aux | awk '!/awk/ && /^rduser.*--scale/{system("kill "$2)}'

Last edited by Firerat; 06-17-2013 at 02:03 AM. Reason: missed a bit ( !/awk/ && )
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
killing runaway processes linuxbeginner1 Linux - General 9 10-29-2010 07:40 PM
Killing running processes hitest Suse/Novell 4 09-03-2005 03:22 PM
Killing processes hongman Linux - Newbie 3 02-28-2005 06:27 PM
killing processes adriaanbw Linux - Software 3 05-24-2003 11:46 PM
Vm: Killing Processes tbhebe Linux - General 3 03-07-2002 10:51 AM


All times are GMT -5. The time now is 01:41 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration