LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   kill process more than 3 hours old from shell command (http://www.linuxquestions.org/questions/programming-9/kill-process-more-than-3-hours-old-from-shell-command-844206/)

saiyen2002 11-14-2010 11:50 AM

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?

Sergei Steshenko 11-14-2010 12:37 PM

Quote:

Originally Posted by saiyen2002 (Post 4158560)
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.

saiyen2002 11-15-2010 10:31 AM

Quote:

Originally Posted by Sergei Steshenko (Post 4158600)
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

phil.d.g 11-15-2010 10:48 AM

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

colucix 11-15-2010 11:06 AM

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".

Sergei Steshenko 11-15-2010 11:47 AM

Quote:

Originally Posted by saiyen2002 (Post 4159555)
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.

saiyen2002 11-16-2010 04:23 AM

What I am having trouble with is finding the correct combination of commands on the linux shell to get this done.

colucix 11-16-2010 04:33 AM

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


All times are GMT -5. The time now is 07:23 AM.