LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-14-2010, 11:50 AM   #1
saiyen2002
Member
 
Registered: Dec 2006
Location: London, UK
Distribution: Centos, Fedora
Posts: 50

Rep: Reputation: 0
kill process more than 3 hours old from shell command


Hi,

My server pretty often becomes full up php processes running which are not needed.

Is there a way to search for and kill any php process that is more than 3 hours old?
 
Old 11-14-2010, 12:37 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by saiyen2002 View Post
Hi,

My server pretty often becomes full up php processes running which are not needed.

Is there a way to search for and kill any php process that is more than 3 hours old?
Are the PHP processes in question seen as regular UNIX processes ? If yes, read

man 1 ps

and pay attention to 'time' (sub)string - one can extract process duration from 'ps' output.
 
Old 11-15-2010, 10:31 AM   #3
saiyen2002
Member
 
Registered: Dec 2006
Location: London, UK
Distribution: Centos, Fedora
Posts: 50

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Are the PHP processes in question seen as regular UNIX processes ? If yes, read

man 1 ps

and pay attention to 'time' (sub)string - one can extract process duration from 'ps' output.
Hi, the time part of the ps output is always 0:00 for the processes that I want to kill. I can tell which processes are very old by looking at the START fied. The script below has the START of NOV12, but the TIME of 0:00.


Code:
root      5619  0.0  0.1 157444  6708 ?        S    Nov12   0:00 /usr/bin/php -q /home/script.php
I want to hunt down script like the above, which is a few days old, and any script more than 3 hours old and kill them
 
Old 11-15-2010, 10:48 AM   #4
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,192

Rep: Reputation: 101Reputation: 101
The question that springs to my mind is why are these processes still running after 3 hours? If you're wanting to kill them, then they are past their usefulness and shut exit gracefully. I would debug the script if I were you.

But, to answer you question the STIME value is what you want to look at. TIME is CPU time, and it may take weeks or months for that value to reach 3:00. STIME will show the time the process started if it started the same day, or it will show the date the process started (as in your example).

So, you want to kill all processes that are showing a date as the STIME as long as it's past 03:00, and you want to compare the STIME of the rest of the processes with the current time to work out which ones you need to kill
 
Old 11-15-2010, 11:06 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
The time shown is the cumulative CPU time. What you need is the elapsed time since the process was started. You can get it using the proper standard format specifier with the -o option:
Code:
ps -o pid=,etime=,cmd= -C php
Take in mind that the etime format is [[dd-]hh:]mm:ss, that is it can contain the number of days separated by an hyphen, and not necessarily the number of elapsed hours (if running since less than 1 hour). You have to consider this when checking for elapsed time > "03:00:00".
 
Old 11-15-2010, 11:47 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by saiyen2002 View Post
Hi, the time part of the ps output is always 0:00 for the processes that I want to kill. I can tell which processes are very old by looking at the START fied. The script below has the START of NOV12, but the TIME of 0:00.


Code:
root      5619  0.0  0.1 157444  6708 ?        S    Nov12   0:00 /usr/bin/php -q /home/script.php
I want to hunt down script like the above, which is a few days old, and any script more than 3 hours old and kill them
So, what's the problem ? You know when the process started and you know current date/time, so you can calculate duration and kill the process if its duration is 3 days or longer.
 
Old 11-16-2010, 04:23 AM   #7
saiyen2002
Member
 
Registered: Dec 2006
Location: London, UK
Distribution: Centos, Fedora
Posts: 50

Original Poster
Rep: Reputation: 0
What I am having trouble with is finding the correct combination of commands on the linux shell to get this done.
 
Old 11-16-2010, 04:33 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Please, show us what have you tried so far. Which ps command are you using, now?
 
  


Reply

Tags
kill, processes, ps, shell


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
Shell Script to kill a process automaticaly brazilian_user Programming 11 06-12-2006 09:09 PM
kill process and its child in a shell dalmassoc Linux - Software 4 03-03-2006 01:57 AM
Need to kill process 48 hours old captnz Programming 5 09-30-2004 03:07 AM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM


All times are GMT -5. The time now is 11:14 AM.

Main Menu
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