Kill IDLE Process using script !!!

Dear Friends ,
I am using DB2 database in AIX 5.3 server . In my server some IDLE process are generated after several times which I need to kill it manually each and every time .

The process I query like following :

root@bagpuss $ ps auxw|sort -r +3|head -10
USER      PID   %CPU  %MEM   SZ  RSS    TTY STAT  STIME  TIME   COMMAND
sharon   1658958  0.1  9.0 218016 214804 - A      Sep 13 38:16 db2agent (idle) 0
dpf      1036486  0.0  1.0 14376 14068   - A      Sep 17  3:10 db2hmon 0
sharon   1822932  0.0  1.0 12196 11608   - A      Sep 12  6:41 db2hmon 0
dpf      1011760  0.0  0.0 9264  9060    - A      Sep 17  3:03 db2hmon 3
dpf      1532116  0.0  0.0 9264  9020    - A      Sep 17  3:04 db2hmon 2
dpf       786672  0.0  0.0 9264  8984    - A      Sep 17  3:02 db2hmon 5
dpf      1077470  0.0  0.0 9264  8968    - A      Sep 17  3:03 db2hmon 1
dpf      1269798  0.0  0.0 9248  9044    - A      Sep 17  2:50 db2hmon 4
db2inst1  454756  0.0  0.0 9012  7120    - A      Jul 19  0:52 db2sysc 0

Now , I want to create a script which helps me to find out the IDLE process and Kill that process automatically using PID.

Would anybody help me to give a sample script to do it ?

I am grateful to you . Waiting for Kind reply ... ....

while true do
sleep 30
ps auxw |grep idle  | kill `awk ' { print $2}'`
done
exit 0

every 30 second the script will check any idle process and kill it

it's maybe ok for you :slight_smile:

---------- Post updated 11-19-09 at 05:10 AM ---------- Previous update was 11-18-09 at 01:54 PM ----------

There might be some issues in this, will try to kill itself...
and what if more than one process are idle...you may get issues...

Actually there is no way we can check our script...

Try:

ps auxw | awk '/idle/&&!/awk/ {print $2}' | xargs -t -I {} kill -9 {}

Put the above in the loop given by zviadi

No this script will try to kill itself after all others are killed.. I have checked and it worked.. :wink: