Operating System - HP-UX
1833875 Members
1863 Online
110063 Solutions
New Discussion

scripts to kill running process longer than 15 minuts

 
SOLVED
Go to solution
Eric  Unix
Frequent Advisor

scripts to kill running process longer than 15 minuts

Dear Sir

I want to kill process which running longer than 15 minuts. Please give me a sample or existed scripts for me. Appreciatively.

Best Regards
Eric
Look forward
10 REPLIES 10
Dennis Handly
Acclaimed Contributor

Re: scripts to kill running process longer than 15 minuts

Scripts by which user?
This will list all root processes with their times:
$ UNIX95=1 ps -u root -opid= -otime=
0 01:48:55
8 00:00
874 1-01:24:52
(Example only: don't kill root processes!)

You would need to remove all times with two colons. Times with one colon you would kill if the minutes were >= 15.
Dennis Handly
Acclaimed Contributor

Re: scripts to kill running process longer than 15 minuts

Note: The above ps(1) returns CPU time used, not wall clock time.
Kenan Erdey
Honored Contributor

Re: scripts to kill running process longer than 15 minuts

Hi,

UNIX95= ps -eo pid,time,args | grep -v grep | grep > /tmp/out
while read a b c
do
sec=`echo $b |cut -c 4-5`
if [ $sec -gt 15 ]
then
kill $a
fi
done < /tmp/out

Kenan.
Computers have lots of memory but no imagination
Eric  Unix
Frequent Advisor

Re: scripts to kill running process longer than 15 minuts

Hello Dennis & Kenan

Appreciate both of your great help.
I try it.

Best Regards
Eric
Look forward
Dennis Handly
Acclaimed Contributor
Solution

Re: scripts to kill running process longer than 15 minuts

>Appreciate both of your great help.

If you are happy with our answers, please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33
Eric  Unix
Frequent Advisor

Re: scripts to kill running process longer than 15 minuts

Dear Sir

UNIX95= ps -eo pid,time,args | grep -v grep | grep

Above command, i dont understand the ' grep -v grep | grep '.
Would you kindly explain the meaning of it for me ? Thanks a lot.

BR
eric
Look forward
James R. Ferguson
Acclaimed Contributor

Re: scripts to kill running process longer than 15 minuts

Hi Eric:

> i dont understand the 'grep -v grep | grep '.

This simply eliminates the 'grep' process from the list of processes you want returned. The 'v' switch means exclude anything that matches.

Using 'grep' to find processes to kill is dangerous. You are not guaranteed to match only what you think. Using the UNIX95 (XPG4) mode of 'ps' allows you to *exactly* match with the '-C' argument.

For example, if we want to look for processes whose name is "mything" or "yourthing" we can do:

# UNIX95= ps -o pid,time,args -C mything -C yourthing

Note that I dropped the '-e' from the 'ps' switches and added '-C mything' and '-C yourthing'.

Notice, too, that the UNIX95= has whitespace following the equal symbol and no semicolon before the 'ps' command. This confines the UNIX95 behavior only to the command line. You don't want to set that mode unless you know what affect it will have.

If you want to eliminate the heading that comes with the 'ps' output, modify the above to:

# UNIX95= ps -o pid= -o time= -o args= -C mything -C yourthing

The "=" symbol after each '-o' argument suppresses that argument's name from any heading line --- quite useful.

See the 'ps' manpages for more information.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: scripts to kill running process longer than 15 minuts

Additionally, it is a VERY BAD idea to kill anything running longer than 15 minutes. All the kernel processes must run forever, and some of your applications may crash if you kill the wrong processes. DO NOT kill anything automatically. Instead, you find the processes that you know are safe to kill and do it manually. Then you fix the problem processes so you don't have to kill them anymore.


Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: scripts to kill running process longer than 15 minuts

>JRF: Notice that the UNIX95= has whitespace following the equal symbol and no semicolon

I've started saying to use UNIX95=1 so nobody gets confused about the whitespace. (Of course the semicolon is another matter.)
Eric  Unix
Frequent Advisor

Re: scripts to kill running process longer than 15 minuts

Dear All Sir

Yes, thank in advance for all of your kind suggestion. I will use it carefully.

Best Regards
Eric
Look forward