PDA

View Full Version : [ubuntu] kill processes with name=x && %mem>y



jcooke22
September 22nd, 2010, 11:11 PM
Hi all,

I have an issue on one of my servers whereby the [normally very helpful] du and tar programs are somehow using up too much or my system resources (du 40% mem, tar 20% mem) and causing problems.

I am after a command which is able to kill a process without knowledge of a PID but by process name e.g. "du" and memory usage e.g. >= 10%.

Something along the lines of:
kill $(pgrep du) grep %MEM > 10
Although I know that is invalid syntax I cannot fathom the correct/best way to achieve this end!

Any helpful hints would be much appreciated!
Thanks

stlsaint
September 23rd, 2010, 12:29 AM
have you not tried:


kill -9 <name_of_process>

Usually you should use a pid here as in


kill -9 <PID>

CharlesA
September 23rd, 2010, 12:43 AM
I usually just use:


ps aux | grep process

Then kill it by the PID. Easier then typing out a long command.

slooksterpsv
September 23rd, 2010, 01:53 AM
ps aux | grep $1 | awk '{print $2,$4,$11}' |
awk '{ if ($2>0.5) print $1,$2,$3;}' |
awk '{ if ($2>3.0) system("echo " $3) }'


I could save that to a file and run it doing the following (assume I name it fmemuse)
sh ./fmemuse chrome
Here's the output:
/opt/google/chrome/chrome

So I'm testing to see if the memory is above 0.5 if it is test to see if its above 3.0 if it is print the location of the file chrome, I could change $3 on the last line to $1 to see its PID or $2 to see it's memory, or even change echo, to kill and do kill $1

LightningCrash
September 23rd, 2010, 01:56 PM
ps aux | grep $1 | awk '{print $2,$4,$11}' |
awk '{ if ($2>0.5) print $1,$2,$3;}' |
awk '{ if ($2>3.0) system("echo " $3) }'


I could save that to a file and run it doing the following (assume I name it fmemuse)
sh ./fmemuse chrome
Here's the output:
/opt/google/chrome/chrome

So I'm testing to see if the memory is above 0.5 if it is test to see if its above 3.0 if it is print the location of the file chrome, I could change $3 on the last line to $1 to see its PID or $2 to see it's memory, or even change echo, to kill and do kill $1
roughly what i was going to say. good job.

slooksterpsv
September 23rd, 2010, 06:21 PM
roughly what i was going to say. good job.

Yeah, but there is a flaw with it, if a program contains the name you type in, it'll kill those programs so if you typed in fmemuse a and accidently pressed enter, all programs with a in it, above memory usage x, would be killed.

LightningCrash
September 23rd, 2010, 07:06 PM
I wouldn't really call that a flaw though... it happily does whatever you tell it to ;)

jcooke22
September 23rd, 2010, 10:19 PM
Thanks slooksterpsv!

I ended up with:
ps aux | grep du | awk '{ if ($4>10) system("kill "$2)}'
Running via a cron job, it seems to work a treat!

Cheers
:)

LightningCrash
September 24th, 2010, 12:54 AM
Thanks slooksterpsv!

I ended up with:
ps aux | grep du | awk '{ if ($4>10) system("kill "$2)}'
Running via a cron job, it seems to work a treat!

Cheers
:)
Now that you've started down the dark side, get a book on Awk ;)

slooksterpsv
September 24th, 2010, 02:28 AM
Now that you've started down the dark side, get a book on Awk ;)

I need one too lol, I did that just by examples and some awk documentation.

SeijiSensei
September 24th, 2010, 03:32 AM
Just a note that you can kill a process by name, rather than PID, using "killall programname" instead of "kill". As the name implies, though, it kills all processes running programname, which may or may not be what you want to do.

killall takes the same switches as kill; -15 is "graceful," while -9 lowers the boom.

BkkBonanza
September 24th, 2010, 05:44 AM
Killing seems so brutal. Couldn't you just set their niceness so they run at low priority? Or is this some hog user on your system you want to rid yourself of?

man renice

LightningCrash
September 24th, 2010, 02:48 PM
Just a note that you can kill a process by name, rather than PID, using "killall programname" instead of "kill". As the name implies, though, it kills all processes running programname, which may or may not be what you want to do.

killall takes the same switches as kill; -15 is "graceful," while -9 lowers the boom.

I would not advise becoming accustomed to using `killall`, as it has drastically different behaviors on other *nix platforms.

pkill is much preferred.

slooksterpsv
September 24th, 2010, 08:29 PM
I would not advise becoming accustomed to using `killall`, as it has drastically different behaviors on other *nix platforms.

pkill is much preferred.

I haven't made scripts that do killall or kill, but I do use those quite a bit with stuck programs. Why pkill? (googling now).

LightningCrash
September 24th, 2010, 08:47 PM
I haven't made scripts that do killall or kill, but I do use those quite a bit with stuck programs. Why pkill? (googling now).

If you're on a Solaris box and you type

killall firefox

It will kill everything on the machine. Solaris' killall doesn't accept processname arguments.