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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-14-2010, 11:50 AM
|
#1
|
|
Member
Registered: Dec 2006
Location: London, UK
Distribution: Centos, Fedora
Posts: 43
Rep:
|
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?
|
|
|
|
11-14-2010, 12:37 PM
|
#2
|
|
Senior Member
Registered: May 2005
Posts: 4,420
|
Quote:
Originally Posted by saiyen2002
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.
|
|
|
|
11-15-2010, 10:31 AM
|
#3
|
|
Member
Registered: Dec 2006
Location: London, UK
Distribution: Centos, Fedora
Posts: 43
Original Poster
Rep:
|
Quote:
Originally Posted by Sergei Steshenko
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
|
|
|
|
11-15-2010, 10:48 AM
|
#4
|
|
Senior Member
Registered: Oct 2004
Posts: 1,188
Rep: 
|
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
|
|
|
|
11-15-2010, 11:06 AM
|
#5
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
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".
|
|
|
|
11-15-2010, 11:47 AM
|
#6
|
|
Senior Member
Registered: May 2005
Posts: 4,420
|
Quote:
Originally Posted by saiyen2002
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.
|
|
|
|
11-16-2010, 04:23 AM
|
#7
|
|
Member
Registered: Dec 2006
Location: London, UK
Distribution: Centos, Fedora
Posts: 43
Original Poster
Rep:
|
What I am having trouble with is finding the correct combination of commands on the linux shell to get this done.
|
|
|
|
11-16-2010, 04:33 AM
|
#8
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Please, show us what have you tried so far. Which ps command are you using, now?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:47 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|