Remove duplicated code for finding a java process given a pidfile

and make it work in busybox.
This commit is contained in:
Fredrik Jönsson 2016-09-23 10:09:54 +02:00
parent d4c7cce7d7
commit 18dce69a78
1 changed files with 23 additions and 38 deletions

View File

@ -366,24 +366,27 @@ invokeJar(){
return $RET return $RET
} }
# Check if ActiveMQ is running # Check if a java process (ActiveMQ) with pid found in file is running
# #
# @RET : 0 => the activemq process is running # @ARG1 : the name of the PID-file
# 1 => process id in $ACTIVEMQ_PIDFILE does not exist anymore # @RET : 0 => the java process is running
# 2 => something is wrong with the pid file # 1 => process id in PID-file does not exist anymore
# 2 => something is wrong with the PID-file
# #
# Note: This function uses globally defined variables # Note: This function uses globally defined variables
# - $ACTIVEMQ_PIDFILE : the name of the pid file # - $ACTIVEMQ_PIDFILE : the name of the pid file
checkRunning(){ checkRunning(){
if [ -f "$ACTIVEMQ_PIDFILE" ]; then local pidfile="${1}"
if [ -z "`cat $ACTIVEMQ_PIDFILE`" ];then
echo "ERROR: Pidfile '$ACTIVEMQ_PIDFILE' exists but contains no pid" if [ -f $pidfile ]; then
if [ -z "`cat "$pidfile"`" ];then
echo "ERROR: Pidfile '$pidfile' exists but contains no pid"
return 2 return 2
fi fi
ACTIVEMQ_PID="`cat ${ACTIVEMQ_PIDFILE}`" local activemq_pid="`cat "$pidfile"`"
RET="`ps -p "${ACTIVEMQ_PID}"|grep java`" local RET="`ps -o "pid,args" | grep "^$activemq_pid\s.*java"`"
if [ -n "$RET" ];then if [ -n "$RET" ];then
return 0; return 0;
else else
@ -394,24 +397,6 @@ checkRunning(){
fi fi
} }
checkStopRunning(){
PID_STOP="${ACTIVEMQ_PIDFILE}.stop"
if [ -f "$PID_STOP" ]; then
if [ -z "`cat $PID_STOP`" ];then
echo "ERROR: Pidfile os stop process '$PID_STOP' exists but contains no pid"
return 2
fi
THEPID=`cat ${PID_STOP}`
RET=`ps -p $THEPID|grep java`
if [ -n "$RET" ];then
return 0;
else
return 1;
fi
else
return 1;
fi
}
# Check if ActiveMQ is running # Check if ActiveMQ is running
# #
@ -423,7 +408,7 @@ checkStopRunning(){
invoke_status(){ invoke_status(){
if ( checkRunning );then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
PID="`cat $ACTIVEMQ_PIDFILE`" PID="`cat $ACTIVEMQ_PIDFILE`"
echo "ActiveMQ is running (pid '$PID')" echo "ActiveMQ is running (pid '$PID')"
exit 0 exit 0
@ -444,7 +429,7 @@ invoke_status(){
# - $ACTIVEMQ_SSL_OPTS : options for SSL encryption # - $ACTIVEMQ_SSL_OPTS : options for SSL encryption
invoke_start(){ invoke_start(){
if ( checkRunning );then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
PID="`cat $ACTIVEMQ_PIDFILE`" PID="`cat $ACTIVEMQ_PIDFILE`"
echo "INFO: Process with pid '$PID' is already running" echo "INFO: Process with pid '$PID' is already running"
exit 0 exit 0
@ -470,7 +455,7 @@ invoke_start(){
# - $ACTIVEMQ_SSL_OPTS : options for SSL encryption # - $ACTIVEMQ_SSL_OPTS : options for SSL encryption
invoke_console(){ invoke_console(){
if ( checkRunning );then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
echo "ERROR: ActiveMQ is already running" echo "ERROR: ActiveMQ is already running"
exit 1 exit 1
fi fi
@ -498,7 +483,7 @@ invoke_console(){
invoke_kill(){ invoke_kill(){
if ( checkRunning ); then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
ACTIVEMQ_PID="`cat ${ACTIVEMQ_PIDFILE}`" ACTIVEMQ_PID="`cat ${ACTIVEMQ_PIDFILE}`"
echo "INFO: sending SIGKILL to pid '$ACTIVEMQ_PID'" echo "INFO: sending SIGKILL to pid '$ACTIVEMQ_PID'"
kill -KILL $ACTIVEMQ_PID kill -KILL $ACTIVEMQ_PID
@ -522,7 +507,7 @@ invoke_kill(){
invoke_stop(){ invoke_stop(){
RET="1" RET="1"
if ( checkRunning );then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS $ACTIVEMQ_SSL_OPTS" ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS $ACTIVEMQ_SSL_OPTS"
COMMANDLINE_ARGS="$COMMANDLINE_ARGS $ACTIVEMQ_SUNJMX_CONTROL" COMMANDLINE_ARGS="$COMMANDLINE_ARGS $ACTIVEMQ_SUNJMX_CONTROL"
@ -535,15 +520,15 @@ invoke_stop(){
i=0 i=0
echo "INFO: Waiting at least $ACTIVEMQ_KILL_MAXSECONDS seconds for regular process termination of pid '$ACTIVEMQ_PID' : " echo "INFO: Waiting at least $ACTIVEMQ_KILL_MAXSECONDS seconds for regular process termination of pid '$ACTIVEMQ_PID' : "
while [ "$i" -le "$ACTIVEMQ_KILL_MAXSECONDS" ]; do while [ "$i" -le "$ACTIVEMQ_KILL_MAXSECONDS" ]; do
if [ ! checkStopRunning ];then if ( ! checkRunning "${ACTIVEMQ_PIDFILE}.stop" ); then
if [ ! checkRunning ]; then if ( ! checkRunning "$ACTIVEMQ_PIDFILE" ); then
echo " FINISHED" echo " FINISHED"
FOUND="1" FOUND="1"
fi fi
break break
fi fi
if (checkRunning);then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
sleep 1 sleep 1
printf "." printf "."
else else
@ -591,12 +576,12 @@ invoke_task(){
ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS $ACTIVEMQ_SSL_OPTS" ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS $ACTIVEMQ_SSL_OPTS"
if [ "$CHECKRUNNING" = "checkforrunning" ];then if [ "$CHECKRUNNING" = "checkforrunning" ];then
if ( ! checkRunning );then if ( ! checkRunning "$ACTIVEMQ_PIDFILE" );then
echo "Activemq is not running." echo "Activemq is not running."
exit 1 exit 1
fi fi
elif [ "$CHECKRUNNING" = "checkfornotrunning" ];then elif [ "$CHECKRUNNING" = "checkfornotrunning" ];then
if ( checkRunning );then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
echo "Activemq is running." echo "Activemq is running."
exit 1 exit 1
fi fi
@ -650,7 +635,7 @@ case "$1" in
invoke_status invoke_status
;; ;;
restart) restart)
if ( checkRunning );then if ( checkRunning "$ACTIVEMQ_PIDFILE" );then
$0 stop $0 stop
echo echo
fi fi