Timeout a Process

By: tjbayly

April 14, 2009 4:38 pm





A customer approached me with a problem: a jboss process needed to be stopped every morning at 3:00am and restarted at 3:30am via cron. There is a shell script for jboss that should accomplish this, but periodically jboss wouldn’t actually stop – resulting in two instances running in the morning.

The solution we came up with was this small bash script that was setup to execute along with the jboss stop shell script, which sets a configurable upper limit for how long to wait before forcefully killing the process corresponding to /home/jboss/jdk1.5.0_12/bin/java.


# Start script configuration options
PROCESS="/home/jboss/jdk1.5.0_12/bin/java"       # The process we are looking for
SLEEPTIME=120         # How many seconds we will wait
# End script configuration options

PID=$(ps auxww | grep $PROCESS | grep -v grep | cut -c10-14 | sed 's/^[ \t]*//;s/[ \t]*$//')

if [ -n "$PID" ]; then                             
  while [ -d /proc/$PID -a $SLEEPTIME -ne 0 ]     
      printf "."                                  
      sleep 1
      SLEEPTIME=`expr $SLEEPTIME - 1`

  if [ -d /proc/$PID ]; then                     
    echo "Killing process $PID"
    kill -9 $PID                                   
  echo "Process $PROCESS is not running"
0 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 5 (0 votes, average: 0.00 out of 5)
You need to be a registered member to rate this post.

Tags: ,
Categories: Enterprise Linux, SUSE Linux Enterprise Server, Technical Solutions

Disclaimer: As with everything else in the SUSE Blog, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.