saidone.org -
just when you thought it couldn't get any better
Main menu
Home
About

Projects
AlfMark

Swamp
Links
ush.it
aghers.org
saidone@ush
gameknot
virtualmagister

Misc
Unless otherwise specified, all contents of this site are licensed under a Creative Commons Attribution-ShareAlike 3.0 Italy License.
JBoss killer service script - posted by saidone on Thu, 26 May 2011 21:03:02 GMT
I was tired of hanged JBoss (especially while running Alfresco) instances, that become unresponsive after a CTRL-C or "service jboss stop".
Here's the definitive, straightforward, pragmatic solution, at least for CentOS / RHEL users: a modified service script that send a TERM signal to JVM, wait a while, and if JVM have not properly exited, send a KILL signal :-)

You can configure the "grace time" in the first lines of the script (line 27):
#time (in seconds) to wait after first SIGTERM signal sent
TIMEOUT=10
To install it, copy the script (after changes to match your JBoss installation/configuration) in /etc/init.d, alongside other services script; then change the executable bit with a:
chmod +x jboss
and register it within the system:
chkconfig --add jboss
To check that is installed properly, try:
chkconfig --list jboss
the output should be:
jboss           0:off   1:off   2:off   3:off   4:off   5:off   6:off
of course you can make the service to be started automagically in the various runlevels, with something like:
chkconfig --level 345 jboss on
That's almost all... here's the download link:
JBoss control script

and the code:
#!/bin/bash
# chkconfig: - 65 36
# description: Jboss Start|Restart|Stop Application Server
# pidfile: /var/run/jboss.pid

#
# JBoss Control Script
#

# Either modify this script for your requirements or just ensure that
# the following variables are set correctly before calling the script.
#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"/opt/jboss"}


#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
JBOSS_USER=${JBOSS_USER:-"jboss"}

#make sure java is in your path
JAVAPTH=${JAVAPTH:-"/opt/java/bin"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"alfresco"}

JBOSS_HOST=192.168.1.227

#time (in seconds) to wait after first SIGTERM signal sent
TIMEOUT=10

#if JBOSS_HOST specified, use -b to bind jboss services to that address
JBOSS_BIND_ADDR=${JBOSS_HOST:+"-b $JBOSS_HOST"}

#define the script to use to start jboss
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF $JBOSS_BIND_ADDR"}

if [ "$JBOSS_USER" = "RUNASIS" ]; then
  SUBIT=""
else
  SUBIT="su - $JBOSS_USER -c "
fi

if [ -n "$JBOSS_CONSOLE" -a ! -d "$JBOSS_CONSOLE" ]; then
  # ensure the file exists
  touch $JBOSS_CONSOLE
  if [ ! -z "$SUBIT" ]; then
    chown $JBOSS_USER $JBOSS_CONSOLE
  fi 
fi

if [ -n "$JBOSS_CONSOLE" -a ! -f "$JBOSS_CONSOLE" ]; then
  echo "WARNING: location for saving console log invalid: $JBOSS_CONSOLE"
  echo "WARNING: ignoring it and using /dev/null"
  JBOSS_CONSOLE="/dev/null"
fi

#define what will be done with the console log
JBOSS_CONSOLE=${JBOSS_CONSOLE:-"/dev/null"}

JBOSS_CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH"

if [ -z "`echo $PATH | grep $JAVAPTH`" ]; then
  export PATH=$PATH:$JAVAPTH
fi

if [ ! -d "$JBOSS_HOME" ]; then
  echo JBOSS_HOME does not exist as a valid directory : $JBOSS_HOME
  exit 1
fi

echo JBOSS_CMD_START = $JBOSS_CMD_START

function procrunning() {
   procid=0
   JBOSSSCRIPT=$(echo $JBOSSSH | awk '{print $1}' | sed 's/\//\\\//g')
   for procid in `/sbin/pidof -x "$JBOSSSCRIPT"`; do
       ps -fp $procid | grep "${JBOSSSH% *}" > /dev/null && pid=$procid
   done
}

stop() {
    pid=0
    procrunning
    if [ $pid = '0' ]; then
        echo -n -e "\nNo JBossas is currently running\n"
        exit 1
    fi

    RETVAL=1

    # If process is still running

    # First, try to kill it nicely
    for id in `ps --ppid $pid | awk '{print $1}' | grep -v "^PID$"`; do
       if [ -z "$SUBIT" ]; then
           kill -15 $id
       else
           $SUBIT "kill -15 $id"
       fi
    done

    sleep=0
    while [ $sleep -lt $TIMEOUT -a $RETVAL -eq 1 ]; do
        echo -n -e "\nwaiting for processes to stop";
        sleep 1 
        sleep=`expr $sleep + 3`
        pid=0
        procrunning
        if [ $pid == '0' ]; then
            RETVAL=0
        fi
    done

    # Still not dead... kill it
    count=0
    pid=0
    procrunning

    if [ $RETVAL != 0 ] ; then
        echo -e "\nTimeout: Shutdown command was sent, but process is still running with PID $id"
	RETVAL=1
	while [ $RETVAL -eq 1 ]; do
	    echo -e "\nSending signal SIGKILL to PID $id"
	    kill -9 $id
	    sleep 5
	    pid=0
            procrunning
            if [ $pid == '0' ]; then
                RETVAL=0
            fi
	done
	echo -e "\nProcess with PID $id killed"
    fi

    echo
    exit 0
}

case "$1" in
start)
    cd $JBOSS_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &
    else
        $SUBIT "$JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &" 
    fi
    ;;
stop)
    stop
    ;;
restart)
    $0 stop
    $0 start
    ;;
*)
    echo "usage: $0 (start|stop|restart|help)"
esac





No comments yet.



Post a new comment

<-- comment title (well, not really mandatory)
<-- your name
<-- your e-mail
<-- multiply 32 by 27

back to home
[[[[[[[ served by kugelmass ]]]]]]]
If we wish to count lines of code, we should not regard them as 'lines produced' but as 'lines spent'. - Edsger Dijkstra