# Kill idle Process using a script

**URL:** <https://community.unix.com/t/kill-idle-process-using-a-script/326115>\
**Category:** Shell Programming and Scripting\
**Created:** [March 22, 2013, 6:08pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115 "2013-03-22T18:08:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaggieL](https://community.unix.com/letter_avatar/maggiel/32/6_449c533f586939b6e5f246632a46421f.png) [@MaggieL](https://community.unix.com/u/MaggieL)\
**Post date:** [March 22, 2013, 6:08pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/1 "2013-03-22T18:08:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hanson44](https://community.unix.com/letter_avatar/hanson44/32/6_449c533f586939b6e5f246632a46421f.png) [@hanson44](https://community.unix.com/u/hanson44)\
**Post date:** [March 22, 2013, 6:50pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/2 "2013-03-22T18:50:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MaggieL](https://community.unix.com/letter_avatar/maggiel/32/6_449c533f586939b6e5f246632a46421f.png) [@MaggieL](https://community.unix.com/u/MaggieL)\
**Post date:** [March 22, 2013, 8:42pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/3 "2013-03-22T20:42:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 23, 2013, 3:06am UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/4 "2013-03-23T03:06:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hanson44](https://community.unix.com/letter_avatar/hanson44/32/6_449c533f586939b6e5f246632a46421f.png) [@hanson44](https://community.unix.com/u/hanson44)\
**Post date:** [March 23, 2013, 3:29am UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/5 "2013-03-23T03:29:49Z")

</div>

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

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![MaggieL](https://community.unix.com/letter_avatar/maggiel/32/6_449c533f586939b6e5f246632a46421f.png) [@MaggieL](https://community.unix.com/u/MaggieL)\
**Post date:** [March 25, 2013, 5:48pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/6 "2013-03-25T17:48:42Z")

</div>

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:

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 25, 2013, 6:05pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/7 "2013-03-25T18:05:34Z")

</div>

```nohighlight
pgrep fglrun

```

?

```nohighlight
pkill fglrun

```

?

---

<div class="post-metadata">

**Author:** ![hanson44](https://community.unix.com/letter_avatar/hanson44/32/6_449c533f586939b6e5f246632a46421f.png) [@hanson44](https://community.unix.com/u/hanson44)\
**Post date:** [March 25, 2013, 8:34pm UTC](https://community.unix.com/t/kill-idle-process-using-a-script/326115/8 "2013-03-25T20:34:40Z")

</div>

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