kill process more than 3 hours old from shell command
ProgrammingThis 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 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.
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
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".
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.