LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-18-2011, 11:42 AM   #1
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Rep: Reputation: 30
Looking for a one liner to filter PID so I can kill it


I'm on a virtual server which is subject to random spikes in load, such that I need a cron job firing every minute to ensure swap isn't exhausted. This sort of works but is a little drastic in that it restarts apache when the free level gets below x. I'm looking to refine that.

Right now if I do ps -A | grep apache2 I get
4575 ? 00:00:00 apache2
7162 ? 00:00:09 apache2
7346 ? 00:00:07 apache2
7357 ? 00:00:05 apache2
7409 ? 00:00:04 apache2
7434 ? 00:00:04 apache2
...

My goal is to kill -9 the pid on the first line. I'm certain this can be done as a one liner, something like ps -A | grep apache2 | take 1st val in 1st line > kill -9 or whatever, but they syntax of this is beyond me and I really don't want to faff about piping to files and parsing that. Any takers please? Thanks.
 
Old 08-18-2011, 11:55 AM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
You can use pidof
Code:
kill -9 $(pidof apache2)
Kind regards
 
Old 08-18-2011, 12:06 PM   #3
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by repo View Post
You can use pidof
Code:
kill -9 $(pidof apache2)
Kind regards
Cheers, but I just tried that and it killed them all! I just want the oldest, which is the one in the first line in the command I gave.
I did see:

#pidof apache2
7513 7490 7489 7482 7481 7477 7469 7460 7448 7434 7409 7357 7346 7162 4575

So just getting the last pid from that line would do it.
 
Old 08-18-2011, 08:59 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could use awk or cut to get the last value & pipe to kill.
This is only addressing the symptoms though; you should really try to fix the root cause.
 
Old 08-18-2011, 10:40 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
This will send a KILL signal to the oldest (earliest started) apache2:
Code:
env LANG=C LC_ALL=C ps -C apache2 -o pid=,lstart= | bash -c '
    OLD=""
    CMD=(true)
    while read PID DATE ; do
        SEC=$[ $(env LANG=C LC_ALL=C date +%s -d "$DATE" 2>/dev/null) -0 ] || continue
        if [ -z "$OLD" ] || [ $OLD -gt $SEC ]; then
           OLD=$SEC
           CMD=(kill -KILL $PID)
        fi
    done
    "${CMD[@]}"
'
It uses Bash, and the date -d command to parse the date string ps provides. (It seems to be irrespective of locale, but I set the locale anyway.)

If you have runaway apache2 processes, consuming excess amounts of memory, you really should be killing based on that (or better yet, set a RAM limit for apache2 processes). To kill any apache2 that consumes (resident set, in RAM) more than X kilobytes, you can use
Code:
ps -C apache2 -o rss=,pid= | awk -v limit=X '($1 > limit) { system("kill -KILL " $2) }'
 
1 members found this post helpful.
Old 08-18-2011, 11:42 PM   #6
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
You can try with the -s option. From the pidof manpage:

Code:
-s     Single shot - this instructs the program to only return one pid.
So maybe this will work:

Code:
kill -9 $(pidof -s apache2)
 
2 members found this post helpful.
  


Reply



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 < file (Feeding kill from file), PID, <, awk, grep jaffd Programming 4 04-09-2010 04:06 PM
kill pid elainelaw Linux - Newbie 1 04-07-2010 08:43 AM
What is the more easier way to check the pid and kill the pid cmx08 Linux - General 5 09-09-2008 10:57 PM
kill pid ekdya Debian 3 04-18-2006 05:04 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 07:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:09 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