定时cronjob调用Java程序

一个部署在Linux环境的Java企业应用后台经常会在大半夜运行很多定时的任务,本篇文章总结下如何使用shell脚本调用Java程序,以备忘。

什么时候Linux才能完美?

1. setupenv.sh

export APP_HOME=`pwd`

if [ -z "$JAVA_HOME" ] ; then
  JAVA=`which java`
  if [ -z "$JAVA" ] ; then
    echo "Cannot find JAVA. Please set your PATH."
    exit 1
  fi
  JAVA_BIN=`dirname $JAVA`
  JAVA_HOME=$JAVA_BIN/..
fi

PATH_SEPARATOR=':'
if [ $OSTYPE = "cygwin32" ] ; then
  PATH_SEPARATOR=';'
fi
if [ $OSTYPE = "cygwin" ] ; then
  PATH_SEPARATOR=';'
fi
JAVA=$JAVA_HOME/bin/java

CLASSPATH=$JAVA_HOME/lib/tools.jar
CLASSPATH=`echo ${APP_HOME}/lib/*.jar | tr ' ' ${PATH_SEPARATOR}`${PATH_SEPARATOR}${CLASSPATH}
CLASSPATH=`echo ${APP_HOME}/lib/*.zip | tr ' ' ${PATH_SEPARATOR}`${PATH_SEPARATOR}${CLASSPATH}
CLASSPATH=`echo ${ANT_HOME}/lib/*.jar | tr ' ' ${PATH_SEPARATOR}`${PATH_SEPARATOR}${CLASSPATH}
CLASSPATH=`echo ${ANT_HOME}/lib/*.zip | tr ' ' ${PATH_SEPARATOR}`${PATH_SEPARATOR}${CLASSPATH}
CLASSPATH=${APP_HOME}/build/classes${PATH_SEPARATOR}${CLASSPATH}
export CLASSPATH

### load other variables from profile
source /etc/profile #set all env

2. app.sh

#!/bin/sh
# -----------------------------------------------------------------------------
# app.sh - Script to run applications
#
# Environment Variable Prequisites
#
#  APP_HOME (Optional) May point at your APP "build" directory.
#                If not present, the current working directory is assumed.
#  APP_OPTS (Optional) Java runtime options used when the "start",
#                "stop", or "run" command is executed.
#  JAVA_HOME    Must point at your Java Development Kit installation.
# -----------------------------------------------------------------------------

# ----- Checking JVM variables -------------------------
export JAVA_PARAM=$1
if [ "$JAVA_PARAM" = "-javaMax" ]
then
    shift
    export memoryX=$1
    shift
else
    export memoryX="1024"
fi

export JAVA_PARAM=$1
if [ "$JAVA_PARAM" = "-jmxHost" ]
then
    shift
    jmxHost=$1
    shift
fi

export JAVA_PARAM=$1
if [ "$JAVA_PARAM" = "-jmxPort" ]
then
    shift
    jmxPort=$1
    shift
fi

# fix env. issues
source /etc/profile

# ----- Verify and Set Required Environment Variables -------------------------
export LANG="en_US.UTF8"

if [ -z "$APP_HOME" ] ; then
    ## resolve links - $0 may be a link to  home
    PRG=$0
    progname=`basename $0`
 
    while [ -h "$PRG" ] ; do
 ls=`ls -ld "$PRG"`
 link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
 else
    PRG="`dirname $PRG`/$link"
 fi
    done
   
    APP_HOME_1=`dirname "$PRG"`/../..
    echo "Guessing APP_HOME from app.sh to ${APP_HOME_1}"
    if [ -d ${APP_HOME_1}/properties ] ; then
    APP_HOME=${APP_HOME_1}
    echo "Setting APP_HOME to $APP_HOME"
 fi
    fi
   
 if [ -z "$APP_OPTS" ] ; then
    APP_OPTS=""
    fi
   
 if [ -z "$JPDA_OPTS" ] ; then
    JPDA_OPTS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
    fi
   
 if [ -z "$JAVA_HOME" ] ; then
 echo You must set JAVA_HOME to point at your Java Development Kit installation
    exit 1
fi
 

# ----- Cygwin Unix Paths Setup -----------------------------------------------

# Cygwin support.  $cygwin _must_ be set to either true or false.
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  *) cygwin=false ;;
esac
 
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$APP_HOME" ] &&
    APP_HOME=`cygpath --unix "$APP_HOME"`
    [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi


# ----- Set Up The Classpath -------------------------------------------

CP=${APP_HOME}/build/classes

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8e00664f04d59b04f7325cbadf4586f8.html