linux脚本(让JAR的config放在外边)

linux脚本(让JAR的config放在外边)

http://hi.baidu.com/liwei_8/blog/item/d9d31f256f274e6434a80f2c.html



linux脚本(让JAR的config放在外边)
2007年12月27日 星期四 10:41
比如您的JAR name为: DatabaseCacheDaemon0510.jar

将此run.sh文件放入JAR的同目录

#!/bin/sh
### ====================================================================== ###
##                                                                          ##
## Daemon Bootstrap Script                                                 ##
##                                                                          ##
### ====================================================================== ###

DIRNAME=`dirname $0`
PROGNAME=`basename $0`
GREP="grep"

# Use the maximum available, or set MAX_FD != -1 to use that
MAX_FD="maximum"

#
# Helper to complain.
#
warn() {
    echo "${PROGNAME}: $*"
}

#
# Helper to puke.
#
die() {
    warn $*
    exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false;
darwin=false;
linux=false;
case "`uname`" in
    CYGWIN*)
        cygwin=true
        ;;

    Darwin*)
        darwin=true
        ;;
       
    Linux)
        linux=true
        ;;
esac

# Read an optional running configuration file
if [ "x$RUN_CONF" = "x" ]; then
    RUN_CONF="$DIRNAME/run.conf"
fi
if [ -r "$RUN_CONF" ]; then
    . "$RUN_CONF"
fi

# Force IPv4 on Linux systems since IPv6 doesn't work correctly with jdk5 and lower
if [ "$linux" = "true" ]; then
   JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
    [ -n "$DAEMON_HOME" ] &&
        DAEMON_HOME=`cygpath --unix "$DAEMON_HOME"`
    [ -n "$JAVA_HOME" ] &&
        JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
    [ -n "$JAVAC_JAR" ] &&
        JAVAC_JAR=`cygpath --unix "$JAVAC_JAR"`
fi

# Setup DAEMON_HOME
if [ "x$DAEMON_HOME" = "x" ]; then
    # get the full path (without any relative bits)
    DAEMON_HOME=`pwd`
fi
export DAEMON_HOME

# Increase the maximum file descriptors if we can
if [ "$cygwin" = "false" ]; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ]; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
     # use the system max
     MAX_FD="$MAX_FD_LIMIT"
fi

ulimit -n $MAX_FD
if [ $? -ne 0 ]; then
     warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
    else
warn "Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# Setup the JVM
if [ "x$JAVA" = "x" ]; then
    if [ "x$JAVA_HOME" != "x" ]; then
JAVA="$JAVA_HOME/bin/java"
    else
JAVA="java"
    fi
fi

# Setup the classpath
#runjar="$DAEMON_HOME/bin/run.jar"
#if [ ! -f "$runjar" ]; then
#    die "Missing required file: $runjar"
#fi
#DAEMON_BOOT_CLASSPATH="$runjar"

DIRLIBS=`ls $DAEMON_HOME/*.jar`

for i in ${DIRLIBS}; do
    if [ "$i" != "${DIRLIBS}" ] ; then
if [ -z "$LOCALCLASSPATH" ] ; then
     LOCALCLASSPATH=$i
else
     LOCALCLASSPATH="$i":$LOCALCLASSPATH
fi
    fi
done

    # For Cygwin, switch paths to Windows format before running java
if $cygwin; then
    LOCALCLASSPATH=`cygpath --path --windows "$LOCALCLASSPATH"`
fi

DAEMON_CLASSPATH=.:$LOCALCLASSPATH


if [ "x$JAVAC_JAR" = "x" ]; then
    JAVAC_JAR_FILE="$JAVA_HOME/lib/tools.jar"
else
    JAVAC_JAR_FILE="$JAVAC_JAR"
fi
if [ ! -f "$JAVAC_JAR_FILE" ]; then
   # MacOSX does not have a seperate tools.jar
   if [ "$darwin" != "true" -a "x$JAVAC_JAR" != "x" ]; then
      warn "Missing file: JAVAC_JAR=$JAVAC_JAR"
      warn "Unexpected results may occur."
   fi
   JAVAC_JAR_FILE=
fi

if [ "x$DAEMON_CLASSPATH" = "x" ]; then
    DAEMON_CLASSPATH="$DAEMON_BOOT_CLASSPATH"
else
    DAEMON_CLASSPATH="$DAEMON_CLASSPATH:$DAEMON_BOOT_CLASSPATH"
fi
if [ "x$JAVAC_JAR_FILE" != "x" ]; then
    DAEMON_CLASSPATH="$DAEMON_CLASSPATH:$JAVAC_JAR_FILE"
fi

# If -server not set in JAVA_OPTS, set it, if supported
SERVER_SET=`echo $JAVA_OPTS | $GREP "\-server"`
if [ "x$SERVER_SET" = "x" ]; then

    # Check for SUN(tm) JVM w/ HotSpot support
    if [ "x$HAS_HOTSPOT" = "x" ]; then
HAS_HOTSPOT=`"$JAVA" -version 2>&1 | $GREP -i HotSpot`
    fi

    # Enable -server if we have Hotspot, unless we can't
    if [ "x$HAS_HOTSPOT" != "x" ]; then
# MacOS does not support -server flag
if [ "$darwin" != "true" ]; then
     JAVA_OPTS="-server $JAVA_OPTS"
fi
    fi
fi

# Setup Daemon Native library path
DAEMON_NATIVE_DIR="$DAEMON_HOME/bin/native"
if [ -d "$DAEMON_NATIVE_DIR" ]; then
    if $cygwin ; then
        export PATH="$DAEMON_NATIVE_DIR:$PATH"
        DAEMON_NATIVE_DIR=`cygpath --dos "$DAEMON_NATIVE_DIR"`
    fi
    if [ "x$LD_LIBRARY_PATH" = "x" ]; then
        LD_LIBRARY_PATH="$DAEMON_NATIVE_DIR"
    else
        LD_LIBRARY_PATH="$DAEMON_NATIVE_DIR:$LD_LIBRARY_PATH"
    fi
    export LD_LIBRARY_PATH
    if [ "x$JAVA_OPTS" = "x" ]; then
        JAVA_OPTS="-Djava.library.path=$DAEMON_NATIVE_DIR"
    else
        JAVA_OPTS="$JAVA_OPTS -Djava.library.path=$DAEMON_NATIVE_DIR"
    fi
fi

# Setup Daemon sepecific properties
JAVA_OPTS="-Dprogram.name=$PROGNAME $JAVA_OPTS"

# Setup the java endorsed dirs
DAEMON_ENDORSED_DIRS="$DAEMON_HOME/lib/endorsed"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
    DAEMON_HOME=`cygpath --path --windows "$DAEMON_HOME"`
    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
    DAEMON_CLASSPATH=`cygpath --path --windows "$DAEMON_CLASSPATH"`
    DAEMON_ENDORSED_DIRS=`cygpath --path --windows "$DAEMON_ENDORSED_DIRS"`
fi

# Display our environment
echo "========================================================================="
echo ""
echo " Daemon Bootstrap Environment"
echo ""
echo " DAEMON_HOME: $DAEMON_HOME"
echo ""
echo " JAVA: $JAVA"
echo ""
echo " JAVA_OPTS: $JAVA_OPTS"
echo ""
echo " CLASSPATH: $DAEMON_CLASSPATH"
echo ""
echo "========================================================================="
echo ""

while true; do
   if [ "x$LAUNCH_DAEMON_IN_BACKGROUND" = "x" ]; then
      # Execute the JVM in the foreground
      "$JAVA" $JAVA_OPTS \
   -Djava.rmi.server.codebase=file:///$DAEMON_HOME/DatabaseCacheDaemon0510.jar \
          -Djava.endorsed.dirs="$DAEMON_ENDORSED_DIRS" \
          -classpath "$DAEMON_CLASSPATH":DatabaseCacheDaemon0510.jar \
         "$@"
      DAEMON_STATUS=$?
   else
      # Execute the JVM in the background
      "$JAVA" $JAVA_OPTS \
   -Djava.rmi.server.codebase=file:///$DAEMON_HOME/DatabaseCacheDaemon0510.jar \
          -Djava.endorsed.dirs="$DAEMON_ENDORSED_DIRS" \
          -classpath "$DAEMON_CLASSPATH" \
         "$@" &
      DAEMON_PID=$!
      # Trap common signals and relay them to the daemon process
      trap "kill -HUP $DAEMON_PID" HUP
      trap "kill -TERM $DAEMON_PID" INT
      trap "kill -QUIT $DAEMON_PID" QUIT
      trap "kill -PIPE $DAEMON_PID" PIPE
      trap "kill -TERM $DAEMON_PID" TERM
      # Wait until the background process exits
      WAIT_STATUS=0
      while [ "$WAIT_STATUS" -ne 127 ]; do
         DAEMON_STATUS=$WAIT_STATUS
         wait $DAEMON_PID 2>/dev/null
         WAIT_STATUS=$?
      done
   fi

   if [ $DAEMON_STATUS -eq 10 ]; then
      echo "Restarting Daemon..."
   else
      exit $DAEMON_STATUS
   fi
done

最后运行您程序中的主类. 如./run.sh com.infobank.superchannel.daemon.databasecache.DatabaseCacheDaemon &

猜你喜欢

转载自jiakechong.iteye.com/blog/1627402