Kill idle Process using a script

Hi,
I need a script that can automatically kill all processes named "webrepn" and "webrebw" if idle for more than 30 minutes.

Then I will have a Cron Job to run the script every night or 2-3 times a day depends on how this script helps.

Right now, I run "ps -ef | grep webrebn" and "kill -9 PID" to kill the idle processes using PID if I see any. Then repeat for "webrepw". The problem is if I forget to do so for couple days, my system will be very slow.
Would anybody help me out here?

I am using DB2 database in AIX 5.3 server.

Any help will be greatly appreciated.

Thank you,
Maggie

How do you detect the processes are "idle" when you view the ps -ef output by eye?

I will get this using ps -ef :
UID PID PPID C STIME TTY TIME CMD

if the stime was like two days or older, or I got a big time, like 122:57, I know it has been idle for a long time, longer than 30 mins.

I would not necessarily call a process with high TIME values "idle".

If you want to kill suspect_process if TIME > 30 minutes, I would do the following:

line=`ps -ef | grep suspect_process | grep -v grep`
pid=`echo $line | cut -f 2 -d " "`
minutes=`echo $line | cut -f 7 -d " " | cut -f 2 -d :`
if [ $minutes -gt 30 ]; then
  kill -9 $pid
  # Record what happened to log file
fi

I don't have the scenario to test this correctly.

TIME on my computer is like 00:00:00 - I cut the middle columns assuming that's the minutes.

STIME would be more awkward to use. On my computer it says something like Mar22. You could certainly use that kind of data, but it's going to be awkward dealing with new month, etc.

If the process is idle, why does it make the computer slow?

I should not use the word "IDLE" then. Normally, our online reports have 30 min time out. However, if the user closed/exit our online report menu improperly, then the process will still running for days even weeks. When more and more of this kind of processes running, our system will slow down.

If I run "ps auxw | grep webrepn", I get this:

USER         PID %CPU %MEM   SZ  RSS    TTY STAT    STIME  TIME COMMAND
userau    307408 13.9  0.0 3256 3200      - A    06:02:22 145:06 fglrun webrepn
userau    499886 11.5  0.0 3256 3200      - A    07:23:58 101:33 fglrun webrepn
userau    610386 11.3  0.0 3256 3200      - A    08:17:22 87:09 fglrun webrepn

Look at under %CPU, 10% for each one, it really will slow down the system.

So two things I need here, 1) to find out the pid of those process, 2) kill them.

Any thoughts?

pgrep fglrun

?

pkill fglrun

?

Yes, it's like a runaway process, not an idle script. Now it makes sense.

To find the pid and kill it, you can use or modify the script I sent before. It automatically finds the pid of the process, and kills it (I think). It's designed to work with ps -ef output. If you want to switch to ps auxw output, just modify the fields retrieved by the cut commands. If you are unsure how to do that, just comment out the kill command and insert "echo $minutes" and "echo $pid" commands and play around with the cut commands until it's finding the right data. Then uncomment the kill line, and run it to verify that it works (kills the runaway process).