• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 876
  • Last Modified:

Shell to kill stale java processes

I would like to create shell that could be called from cron to find all running java processes by their given PID that are older than 4 hours from the current time:
ps -ef |grep java....(time > 4hrs + current)
Moreso, I would like to have the shell kill all said processes .
I'm up on the cron, but having trouble developing the shell.  Any ideas would be greatly appreciated.
0
TyBay
Asked:
TyBay
  • 5
  • 5
  • 4
2 Solutions
 
ahoffmannCommented:
which OS?

i.g. you better go with a script which starts your processes and kills them after 4 hours 'cause it's hard to identify when a process was started (depending on your OS)
0
 
yuzhCommented:
You can use:

   ps -e -o pid,time,args | grep java > output-file

   output-file has the format
   PID  TIME COMMAND-WITH-ARGS

   process the file, if the 2nd colum > 4:00 then kill the PID.
0
 
yuzhCommented:
PS: you can replace : with a DOT to make life easier for you ,eg:

ps -e -o pid,time,args | grep java | tr ":" "." | awk '{if $2 > 4.0) {print $1} }' > killlist

kill -9 `cat killlist`
rm  killlist`

Or

kill -9 `ps -e -o pid,time,args | grep java | tr ":" "." | awk '{if $2 > 4.0) {print $1} }' `

Enough infor for you to make you own script.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ahoffmannCommented:
ps -o time
is the system or user time, I'd use
  ps -e -o pid,start,args
(if your ps supports this)
and then you have fun with parsing the result of the START column ;-)
0
 
yuzhCommented:
You might be able to use:
   ps -e -o pid,etime,args | grep java

   Where:
                etime     In the POSIX locale, the elapsed  time  since  the
               process was started, in the form:
                              [[dd-]hh:]mm:ss

   It depends on your version of OS, Solaris /usr/bin/ps support it. do a:
   man ps
   on your system to find out.

0
 
TyBayAuthor Commented:
Running SunOS 5.8

Many Thanks thus far...
0
 
ahoffmannCommented:
for Solaris it is -o etime as yuzh said, and have more fun with the format ;-)
0
 
TyBayAuthor Commented:
Cheers,
Many Thanks to All...
0
 
TyBayAuthor Commented:
One wrinkle:
I don't want to kill the mothership java processes for tomcat.  I thought to write out hte pid upon start up and include and exclusion of killlist according to the PID - but I also noticed that the process is distinguished according to a string in the arg as seen below:
"-Dant.home="
Could I inlcude another if statement to excludein the even that the arg matches above?
0
 
ahoffmannCommented:
ps -e -o pid,ppid,etime,args
then kill only those which have the same ppid, something like:
0
 
TyBayAuthor Commented:
Excellent.
Thanks...
0
 
yuzhCommented:
split ?
0
 
TyBayAuthor Commented:
Please close this one - prior to closing see below updates - Thanks and sorry for not doing so earlier...
Grade = A
300pts to yuzh
200pts to ahoffman
0
 
yuzhCommented:
Thanks for both of you, have a nice weekend!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now