定时cronjob调用Java程序(2)

CP=`echo ${APP_HOME}/lib/*.jar | tr ' ' :`:${CP}
CP=`echo ${APP_HOME}/lib/*.zip | tr ' ' :`:${CP}
if [ -f "$JAVA_HOME/lib/tools.jar" ] ; then
  CP=$CP:"$JAVA_HOME/lib/tools.jar"
fi

# ----- Cygwin Windows Paths Setup --------------------------------------------

# convert the existing path to windows
if $cygwin ; then
  CP=`cygpath --path --windows "$CP"`
  APP_HOME=`cygpath --path --windows "$APP_HOME"`
  JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
fi


# ----- Execute The Requested Command -----------------------------------------

echo "Using CLASSPATH:    $CP"
echo "Using APP_HOME: $APP_HOME"
echo "Using JAVA_HOME:    $JAVA_HOME"

JMX_OPTS=""

if [ "$jmxHost" != "" ]
then
    JMX_OPTS=" -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname="$jmxHost
fi

if [ "$jmxPort" != "" ]
then
    JMX_OPTS=$JMX_OPTS" -Dcom.sun.management.jmxremote.port="$jmxPort
fi

export JAVA_OPTS="-verbosegc -XX:+PrintGCDetails -XX:+PrintTenuringDistribution"

JAVA_OPTS=$JAVA_OPTS" "-server" "-Xms256M" "-Xmx$memoryX"M "-XX:NewSize=128m" "-XX:MaxNewSize=128m" "-XX:+UseConcMarkSweepGC" "-Xconcurrentio" "-Xnoclassgc""
JAVA_OPTS=$JAVA_OPTS" "$JMX_OPTS

JAVA_OPTS=$JAVA_OPTS" "-DOracle.jdbc.V8Compatible=true

$JAVA_HOME/bin/java ${JAVA_OPTS} $APP_OPTS -classpath $CP -Dapp.base=$APP_BASE  -Dapp.home=$APP_HOME -Djava.library.path=$HOME/JMagick/lib $@

3. runMyJob.sh

#!/bin/sh
#=============================================================================
# Desc  : MyJob
# 00 03 * * * /home/app/src/scripts/runMyJob.sh 0 >> /home/app/runMyJob0.log 2>&1
#=============================================================================

export APP_HOME=$HOME/app
cd $HOME
. .bashrc
cd $APP_HOME
. setupenv.sh

LOCKFILE="$HOME/MyJob$1.lock";
CONCURRENT_WARNING="MyJob lock file found: $LOCKFILE \n Probably a previous job is still executing, please wait for a while and try again later. \n";
init_day=`date +%Y%m%d%H%M`;

logfile="$HOME/MyJob$1_$init_day.log";

if [ -f $LOCKFILE ]
then
    echo "$CONCURRENT_WARNING";
    exit 1;
fi

#create a lock file to prevent concurrent job requests
touch $LOCKFILE

echo ">>>>>START at `date`"
echo ">>> execute the business ...."
$APP_HOME/src/scripts/app.sh -javaMax 2049 com.zdp.MyJob $1 >> $logfile 2>&1
echo ">>> $1 ....  "

#gzip log file
gzip $logfile;

#move the log to the logs directory
cd $HOME
mv $logfile.gz $HOME/logs/

# remove lock file
rm -rf $LOCKFILE

echo ">>>>> END at `date`"

4. Java程序:MyJob.java

package com.zdp;

public class MyJob {
    private static String MODE = "0";
 public static void main(String args[]) throws Exception {
  try {
   System.out.println("----------------- START -----------------");
   long begin = System.currentTimeMillis();
   if (args.length > 0) {
    MODE = args[0];
   }
   
   // main logic
   
   long end = System.currentTimeMillis();
   System.out.println("<<<<<<<<<<<<<<<<<<<<  Total spent time: " + (end - begin) / 1000 + "s  >>>>>>>>>>>>>>>>>>>>");
   System.out.println("----------------- END -------------------");
  } catch (Exception e) {
   logger.error(e.getMessage(), e);
   System.exit(1);
  } finally{
   System.exit(0);
  }
 }
 
}

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

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