thomas2004ch |
12-02-2013 03:27 AM |
[bash Script] Question about usage of $0
I have a script (very long) which used to start the Jboss server. What I want is: before the Jboss is started, it does some cleanup job. But as I run the script, it doesn't work. I see the problem is by using the $0 cleanup in the script.
Code:
#!/bin/bash
#
#
# chkconfig: 3 98 02
JBOSS_USER=test
. /home/$JBOSS_USER/.bash_profile
#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"/usr/local/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:-"/usr/local/jdk/bin"}
#configuration to use, usually one of 'minimal', 'default', 'all', 'production'
JBOSS_CONF=${JBOSS_CONF:-"default"}
#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" -u $MULTICAST}
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF $JBOSS_BIND_ADDR"}
# Clean up cache and save the gc.log, console.log etc.
$0 cleanup
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_CONSOLE="/var/log/jboss/$JBOSS_USER/console.log"
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 120 -a $RETVAL -eq 1 ]; do
echo -n -e "\nwaiting for processes to stop";
sleep 10
sleep=`expr $sleep + 10`
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 $pid"
exit 1
fi
echo
exit 0
}
cleanup() {
pid=0
procrunning
if [ $pid = '0' ]; then
echo -n -e "\nNo JBossas is currently running\n"
exit 1
else
echo -n -e "\nJboss is still running. You have to stop the Jboss first.\n"
fi
}
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
;;
cleanup)
cleanup
;;
*)
echo "usage: $0 (start|stop|restart|help|cleanup)"
esac
|