LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   killing multiple processes (https://www.linuxquestions.org/questions/linux-general-1/killing-multiple-processes-4175466040/)

michael.barnes 06-14-2013 01:37 PM

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

TobiSGD 06-14-2013 02:04 PM

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.

michael.barnes 06-14-2013 02:14 PM

I think I understand that. So what do I do to fix it?

TobiSGD 06-14-2013 02:32 PM

Add a
Code:

grep -v grep
into the pipe after the grep for scale.

michael.barnes 06-14-2013 03:59 PM

That was the secret. Works fine now. Thanks.

unSpawn 06-14-2013 05:59 PM

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


chrism01 06-17-2013 12:39 AM

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.

Firerat 06-17-2013 12:53 AM

Since you are already using awk, get it to do the kill

Code:

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


All times are GMT -5. The time now is 10:15 PM.