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