SUSE Conversations


Timeout a Process



By: tjbayly

April 14, 2009 4:38 pm

Reads:216

Comments:0

Rating:0

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.

#!/bin/bash

# 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 ]     
    do
      printf "."                                  
      sleep 1
      SLEEPTIME=`expr $SLEEPTIME - 1`
    done

  if [ -d /proc/$PID ]; then                     
    echo "Killing process $PID"
    kill -9 $PID                                   
  fi
else
  echo "Process $PROCESS is not running"
fi
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

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

Disclaimer: As with everything else at SUSE Conversations, 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.

Comment

RSS