Timed Cronjob Call Java program

Source: Internet
Author: User

A Java enterprise application that is deployed in a Linux environment often runs many timed tasks in the middle of the night, and this article summarizes how to use shell scripts to invoke Java programs for forgetting.

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/. Fipath_separator= ': ' If [$OSTYPE = ' cygwin32 ']; Then path_separator= '; ' fiif [$OSTYPE = "Cygwin"]; Then path_separator= '; ' Fijava= $JAVA _home/bin/javaclasspath= $JAVA _home/lib/tools.jarclasspath= ' 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 Profilesource/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 was assumed.# app_opts (Optional) Java runtime options used when th  E "Start", # "Stop", or "Run" command is executed.# Java_home must point at your JAVA development Kit installation.#-----------------------------------------------------------------------------#-----Checking JVM     Variables-------------------------export java_param=$1if ["$JAVA _param" = "-javamax"]then shift export memoryx=$1    Shiftelse export memoryx= "1024x768" Fiexport java_param=$1if ["$JAVA _param" = "-jmxhost"]then shift jmxhost=$1 Shiftfiexport java_param=$1if ["$JAVA _param" = "-jmxport"]then shift jmxport=$1 shiftfi# fix env. issuessource/etc/profile#-----Verify and Set Required Environment Variables-------------------------export lang= "en_US. UTF8 "If [-Z" $APP _home "]; Then # # Resolve Links-$ May is a link to home prg=$0 progname= ' basename $ ' while [-H ' $PRG]; Dols= ' 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] $JAV A_home "]; Thenecho must set Java_home to point at your JAVA development Kit installation Exit 1fi#-----Cygwin Unix Paths Se Tup-----------------------------------------------# Cygwin Support.  $cygwin _must_ is set to either True or False.case "' uname '" in cygwin*) cygwin=true;; *) Cygwin=false;; ESAC # for Cygwin, ensure paths was in UNIX format before anything is Touchedif $cygwin; Then [-N "$APP _home"] && app_home= ' cygpath--unix "$APP _home" ' [-N ' $JAVA _home "] && Java_hom E= ' Cygpath--unix "$JAVA _home" ' fi#-----Set up the Classpath-------------------------------------------cp=${app_home }/build/classescp= ' 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 WindowsIf $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-----------------------------------------EchoUsing 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= "$jmxHostfiif [" $jmxPort "! =" "]then jmx_opts= $JMX _opts"-D com.sun.management.jmxremote.port= "$jmxPortfiexport 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 _optsjava_ 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 [email protected]
3. runmyjob.sh

#!/bin/sh#=============================================================================# desc:myjob# * * * */hom e/app/src/scripts/runmyjob.sh 0 >>/home/app/runmyjob0.log 2>&1#===================================== ========================================export app_home= $HOME/appcd $HOME. . BASHRCCD $APP _home. setupenv.shlockfile= "$HOME/myjob$1.lock"; concurrent_warning= "MyJob lock file found: $LOCKFILE \ n Probably a previous job is still executing, please wait for a whil E 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 Requeststouch $LOCKFILEecho ">>>>>start at ' Date '" EC Ho ">>> execute the business ..." $APP _home/src/scripts/app.sh-javamax 2049 com.zdp.MyJob $ >> $logfile 2  >&1echo ">>> .... "#gzip log filegzip $logfile; #move the log to the logs DIRECTORYCD $HOMEmV $logfile. GZ $HOME/logs/# Remove lock filerm-rf $LOCKFILEecho ">>>>> END at ' Date '" 
4. Java Program: Myjob.java

Package Com.zdp;public class MyJob {    private static string MODE = "0";p ublic static void Main (String args[]) throws Ex ception {try {System.out.println ("-----------------START-----------------"); Long begin = System.currenttimemillis () if (Args.length > 0) {MODE = args[0];} Main Logiclong end = System.currenttimemillis (); System.out.println ("<<<<<<<<<<<<<<<<<<<< Total  Spent time: "+ (End-begin)/+ +" s  >>>>>>>>>>>>>>>>>>& Gt;> "); System.out.println ("-----------------END-------------------");} catch (Exception e) {logger.error (E.getmessage (), E); System.exit (1);} Finally{system.exit (0);}}

Timed Cronjob Call Java program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.