#!/bin/sh # # PEtALS ESB administration script # # DESCRIPTION # # The main function of this script is to start, suspend or stop the PEtALS # ESB. These operations are all performed in a "robust" manner in order to # hide some unexpected behaviors of the ESB when starting or stopping. # # NOTE # This script should be simplified as soon as the problems with # components lifecycle management are fixed ############################################################################# #debug mode #set -xv if [ ! -d "$PETALS_HOME" ] then TARGET_DIRECTORY=`dirname $0` TARGET_DIRECTORY=`cd $TARGET_DIRECTORY;pwd` if [ ! -x "$TARGET_DIRECTORY"/setenv.sh ] then echo "Cannot find $TARGET_DIRECTORY/setenv.sh script." echo "This file is required to start PEtALS." exit 1 fi else TARGET_DIRECTORY="$PETALS_HOME/bin" fi . "$TARGET_DIRECTORY"/setenv.sh # Print the usage message ############################################################################# print_usage() { cat - <&2 Usage: $NAME {start|console|suspend|stop|status} start Start $NAME in server mode console Start $NAME in console mode debug Start $NAME in debug mode stop Stop $NAME without undeploying the services shutdown Undeploy all the services, uninstall all the components and stop $NAME status Print $NAME status EOF } # Exit with an error message # # Arguments # $* the error message ############################################################################# die() { [ -z "$*" ] || echo "$NAME: ERROR: $*" >&2 exit 1 } # Exit and indicate success ############################################################################# success() { echo "OK" exit 0 } # Exit and indicates failure ############################################################################# failure() { echo "FAILED" exit 1 } # Exit and indicates success after an abnormal behavior ############################################################################# passed() { echo "PASSED" exit 3 } # Start the PEtALS JVM as a daemon process, output redirected to # petals-out.log ############################################################################# petals_daemon() { mkdir -p $PETALS_HOME/logs nohup $PETALS_EXEC $* > $PETALS_HOME/logs/petals-out.log 2>&1 & } # Print PID of PEtALS processes on STDOUT ############################################################################# petals_pid() { ps -eo "%p|%c|%a" \ | sed -ne 's:^ *\([0-9]*\)|java *|.*server\.jar.*$:\1:p' } # Print the current PEtALS status on STDOUT with the following meanings: # running petals is running AND the lock file exists # stopped petals process not found AND the lock file does not exist # unknown the lock file exists while the java process terminated OR # the java process still exist while the lock file is not found # # Arguments # $1 the expected status (optional), status is not printed when provided # # Return # 0 if the current status and the expected status are the same # 1 otherwise ############################################################################# petals_status() { lock="$PETALS_HOME/locked" pid="`petals_pid`" status="unknown" if [ -f "$lock" -a -d "/proc/$pid" -a ! -z "$pid" ]; then status="running" elif [ ! -f "$lock" -a -z "$pid" ]; then status="stopped" fi [ -z "$1" ] && echo "$status" test "$status" = "$1" } # Blocks execution until the given condition become true or the given timeout # expires. The condition is polled every second. # # Arguments # $1 the condition expression # $2 the timeout in seconds ############################################################################# petals_wait_for() { status="${1?}" timeout="${2:-"30"}" for tick in `yes | sed "${timeout}q"`; do petals_status "$1" && return 0 sleep 1 echo -n "." done return 1 } # Start PEtALS with the given options # # Arguments # $1 Description # $2 Expected status (running, stopped) # $3 Timeout in seconds # $4 Options for the JVM # ############################################################################# petals_action() { echo -n "$1 $NAME: " petals_status "$2" && return 0 petals_daemon "$4" petals_wait_for "$2" $3 } # Kill every PEtALS processes with the given signal # # Arguments # $1 the signal name or number (see kill -l) # ############################################################################# petals_kill() { sig="${1:-"TERM"}" petals_pid | xargs -r kill -$sig 2>&1 > /dev/null return 0 } # Force petals to be stopped ############################################################################# petals_force_stop() { petals_kill TERM if ! petals_wait_for "stopped" 30; then petals_kill KILL if petals_wait_for "stopped" 30; then rm -rf $PETALS_HOME/locked $PETALS_HOME/work/* fi fi petals_status "stopped" } # Force petals to be shutdowned ############################################################################# petals_force_shutdown() { if petals_force_stop; then rm -f $PETALS_HOME/repository/system-state.xml rm -rf $PETALS_HOME/install/* rm -rf $PETALS_HOME/installed/* rm -rf $PETALS_HOME/repository/components/* rm -rf $PETALS_HOME/repository/service-assemblies/* rm -rf $PETALS_HOME/repository/shared-libraries/* fi petals_status "stopped" } ############################################################################# trap "die 'caught an interruption signal'" INT QUIT KILL TERM HUP [ -d "$JAVA_HOME" ] || die "$NAME: JAVA_HOME: environment variable not set or directory not found" [ -d "$PETALS_HOME" ] || die "$NAME: PETALS_HOME: environment variable not set or directory not found" if [ $# -ne 1 ] ; then print_usage exit 2 fi case "$1" in start) if petals_action "Starting" "running" 600 "start"; then success else failure fi ;; debug) $PETALS_DEBUG_EXEC start ;; console) $PETALS_EXEC -console start ;; stop) if petals_action "Stop" "stopped" 30 "stop"; then success elif petals_force_stop; then passed else failure fi ;; shutdown) if petals_action "Shutdown" "stopped" 30 "shutdown"; then success elif petals_force_shutdown; then passed else failure fi ;; status) case "`petals_status`" in running) echo "$NAME is RUNNING (pid `petals_pid`)" ;; stopped) echo "$NAME is STOPPED" ;; *) echo "$NAME status is UNKNOWN" esac ;; *) die "$1: unknown command" esac exit 0