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? |
Quote:
man 1 ps and pay attention to 'time' (sub)string - one can extract process duration from 'ps' output. |
Quote:
Code:
root 5619 0.0 0.1 157444 6708 ? S Nov12 0:00 /usr/bin/php -q /home/script.php |
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 |
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 |
Quote:
|
What I am having trouble with is finding the correct combination of commands on the linux shell to get this done.
|
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 11:00 AM. |