linux下運行java程序 和 優化的幾種方式

1..直接nohup後臺運行jar包

創建test.sh 文件內容如下
nohup java -jar ZL_LRC_RES.jar >/dev/null  &

注意步驟
1:導出可執行的jar包,設置好main方法
2:上傳的sh腳本注意編碼
vi test.sh
set fileformat=unix
3:./test.sh執行
4:ps -ef|grep java 查看進程
5:查看輸出


2..通過ant導出java程序執行

ant腳本導出jar包後傳到linux服務器

<project name="build" default="build">


<property environment="env" />


<property file="component.properties" />


<property name="debug" value="true" />


<property name="jdk1.6" value="C:/Program Files/Java/jdk1.7.0_01/bin/javac" />
<!-- 設置jdk1.6編譯器的全路徑 -->
<property name="javaCompiler" value="${jdk1.6}" />
<!-- 編譯時使用的javac -->




<path id="cmp.classpath">
<fileset dir="WebRoot/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>
<pathelement path="${java.class.path}" />
</path>


<target name="init">
<echo message="Processing component ${cmp.name}..." />
</target>




<target name="cleanup" depends="init" description="Cleans build directories">
<echo message="Cleaning..." />
<delete includeEmptyDirs="true" failοnerrοr="true">
<fileset dir="build" includes="**/*" />
</delete>
</target>


<target name="compile.check" depends="init">
<mkdir dir="build/depcache" />
<depend srcdir="src/" destdir="build/src" classpath="${cmp.classpath.compile}" cache="build/depcache" closure="yes" />
<depend srcdir="test/" destdir="build/test" classpath="${cmp.classpath.compile}${cmp.classpath.test}" cache="build/depcache" closure="yes" />
<condition property="recompile" value="true">
<not>
<and>
<uptodate>
<srcfiles dir="." includes="src/**/*.java" />
<mapper type="glob" from="*.java" to="build/*.class" />
</uptodate>
<uptodate>
<srcfiles dir="." includes="src/**/*" excludes="**/*.java" />
<mapper type="glob" from="*" to="build/*" />
</uptodate>
</and>
</not>
</condition>
</target>


<target name="compile" depends="compile.check" if="recompile">
<echo message="Compiling sources..." />
<mkdir dir="build/src" />
<!-- 編譯src.home下的所有java文件。fork被設置爲true,這樣可指定編譯使用的JDK,此處使用jdk1.5。必須根據java源文件的編碼來設置encoding。 -->


<javac destdir="build/src" source="1.6" target="1.6" debug="${debug}">
<src path="src/"/>
<!--src path="src/main/resources" /-->
<classpath>
<pathelement path="${cmp.classpath.compile}" />
<path refid="cmp.classpath" />
</classpath>
</javac>
<copy todir="build/src">
<fileset dir="src" excludes="**/*.java" />
<!--fileset dir="src/main/resources" excludes="**/*.java" /-->
</copy>
<echo message="Compiling tests..." />
<mkdir dir="build/test" />
<javac destdir="build/test" source="1.6" target="1.6" debug="${debug}">
<src path="test" />
<!--src path="src/test/resources" /-->
<classpath>
<pathelement location="build/src" />
<pathelement path="${cmp.classpath.test}" />
<!--pathelement path="${jar_path}" />
<pathelement path="${cmp.classpath.compile}" /-->
<path refid="cmp.classpath" />
</classpath>
</javac>
<copy todir="build/test">
<fileset dir="test" excludes="**/*.java" />
<!--fileset dir="src/test/resources" excludes="**/*.java" /-->
</copy>
</target>




<target name="package.check" depends="compile">
<condition property="repackage">
<not>
<uptodate>
<srcfiles dir="build" includes="src/**/*" />
<mapper type="merge" to="${cmp.name}.jar" />
</uptodate>
</not>
</condition>
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm" />
</tstamp>
</target>




<target name="package" depends="package.check" if="repackage">
<echo message="Creating ${cmp.name}.jar..." />
<jar destfile="build/${cmp.name}.jar" index="true">
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Class-Path" value="${cmp.classpath.package}" />
<attribute name="Implementation-Title" value="${cmp.name}" />
<attribute name="Implementation-Version" value="${cmp.name} built on ${TODAY}" />
</manifest>
<fileset dir="build/src" />
</jar>
</target>




<target name="build" depends="package" description="Builds component" />

<!-- Packages files tarball for Linux  WebRoot/WEB-INF/lib-->
<target name="cpid.linux.package" depends="build" description="Packages to Linux">

<property name="package.name" value="build/cpid-install.tar.gz" />
<mkdir dir="build/install"/>
<mkdir dir="build/install/lib"/>
<mkdir dir="build/install/config"/>
<echo message="Making a tar file ${package.name}" />
<!-- Copy the needed files into build dir -->
<echo message="Copy files into build directory" />
<copy todir="build/install/lib" overwrite="true">
<fileset dir="WebRoot/WEB-INF/lib/" includes="**/*.jar" />
<fileset file="build/*.jar"></fileset>
</copy>


<copy todir="build/install/config" overwrite="true">
<fileset dir="config/cpid/" includes="**/*" />
</copy>
<copy todir="build/install/" overwrite="true">
<fileset dir="shell/cpid/" includes="**/*" />
</copy>
<!-- Make the package now -->
<tar destfile="${package.name}" longfile="gnu" basedir="build/install/" compression="gzip" />
<!--
<delete includeEmptyDirs="true" failοnerrοr="false" quiet="true">
<fileset dir="${build.dir}" includes="**/*" />
</delete>
-->
</target>

<!-- Packages files tarball for Linux  WebRoot/WEB-INF/lib-->
<target name="top.linux.package" depends="build" description="Packages to Linux">

<property name="package.name" value="build/top-install.tar.gz" />
<delete includeEmptyDirs="true" failοnerrοr="false" quiet="true">
<fileset dir="build/install" includes="**/*" />
</delete>
<mkdir dir="build/install"/>
<mkdir dir="build/install/lib"/>
<mkdir dir="build/install/config"/>
<echo message="Making a tar file ${package.name}" />
<!-- Copy the needed files into build dir -->
<echo message="Copy files into build directory" />
<copy todir="build/install/lib" overwrite="true">
<fileset file="build/*.jar"></fileset>
</copy>


<copy todir="build/install/config" overwrite="true">
<fileset dir="config/top/" includes="**/*" />
</copy>
<copy todir="build/install/" overwrite="true">
<fileset dir="shell/top/" includes="**/*" />
</copy>
<!-- Make the package now -->
<tar destfile="${package.name}" longfile="gnu" basedir="build/install/" compression="gzip" />
<!--
<delete includeEmptyDirs="true" failοnerrοr="false" quiet="true">
<fileset dir="${build.dir}" includes="**/*" />
</delete>
-->
</target>


</project>


執行的sh腳本

#! /bin/ksh
. ${HOME}/.profile


# set 配置文件地址
cp="/scmgt/shell/gt/config"

# set ant導出的jar包
cp="$cp:/scmgt/shell/gt/productServer.jar"

#set 要用到的服務器上第三方公用jar包 ,如果jar包中有productServer.jar會覆蓋之前的,所以要grep -v 
basedir=/scmgt/productServer/jboss7/standalone/deployments/productServer.war/WEB-INF/
libdir=$basedir/lib
for file in `ls -1 $libdir/*.jar|grep -v "productServer.jar"`
do
cp="$cp:$file"
done
echo $cp
export CLASSPATH=$cp

#運行腳本${1} 是傳入參數 如:./test.sh  3
java -Ddefault.client.encoding="GBK" -Dfile.encoding="GBK" -Duser.language="Zh" -Duser.region="CN" -Duser.timezone=GMT+8 com.sitech.nbc.product.service.impl.ChTFtpMusicFileServiceImpl ${1}



3..通過crontab -e 運行java定時任務

1.crontab -e 進入定時文件,編輯

## add by gt 每隔5分鐘執行如下腳本
*/5 * * * * sh /scmgt/shell/FtpStart.sh >/dev/null 2>&1

2.FtpStart.sh定時要執行的sh腳本,

(做了防止重複java的機制,如果存在刪除正在執行的,執行新的)

同時在執行sh腳本時候會輸出日誌

#! /bin/ksh
. ${HOME}/.profile


count=`ps -ef|grep "FtpColoringMain.sh"|grep -v grep|wc -l`


if [ ${count} -lt 1 ]
then
   nohup sh /scmgt/shell/gt/FtpColoringMain.sh 0 2>&1 >/scmgt/shell/gt/FtpColoringMain.log  &
fi


kill -9 `ps -ef|grep "FtpWalkmanMain.sh"|grep -v grep|awk '{print $2}'`
count=`ps -ef|grep "FtpWalkmanMain.sh"|grep -v grep|wc -l`


if [ ${count} -lt 1 ]
then
   nohup sh /scmgt/shell/gt/FtpWalkmanMain.sh 3 2>&1 >/scmgt/shell/gt/ftpwalkman.log  &
fi


3.FtpWalkmanMain.sh

#! /bin/ksh
. ${HOME}/.profile


# set classpath
cp="/scmgt/shell/gt/config"


basedir=/scmgt/productServer/jboss7/standalone/deployments/productServer.war/WEB-INF/
libdir=$basedir/lib
cp="$cp:/scmgt/shell/gt/productServer.jar"


for file in `ls -1 $libdir/*.jar|grep -v "productServer.jar"`
do
cp="$cp:$file"
done
echo $cp
export CLASSPATH=$cp
java -Ddefault.client.encoding="GBK" -Dfile.encoding="GBK" -Duser.language="Zh" -Duser.region="CN" -Duser.timezone=GMT+8 com.sitech.nbc.product.service.impl.ChTFtpMusicFileServiceImpl ${1}

4.gc優化執行java

#! /bin/ksh
. ${HOME}/.profile


# set classpath
cp=/scmgt/shell/gt/config


#set main jar
mianjar=/scmgt/shell/gt/test/productServers.jar
cp=$cp:$mianjar


#set other jar
basedir=/scmgt/productServer/jboss7/standalone/deployments/productServer.war/WEB-INF/
libdir=$basedir/lib
for file in `ls -1 $libdir/*.jar|grep -v "productServer.jar"`
do
cp=$cp:$file
done




MAIN_CLASS=com.sitech.nbc.util.est


JAVA_OPTS="-Xms1024m -Xmx1024m  -DSTATUS=$status -Ddefault.client.encoding="GBK" -Dfile.encoding="GBK" -Duser.language="Zh" -Duser.region="CN" -Duser.timezone=GMT+8"


# *********** concurrent mark-sweep GC options  ***********
    JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC"


    #Ratio between eden and survivor spaces
    JAVA_OPTS="$JAVA_OPTS -XX:PermSize=256m"
    JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=10"


    #Sets the threshold percentage of the used heap in the old generation at which the CMS collection takes place.


    JAVA_OPTS="$JAVA_OPTS -XX:CMSInitiatingOccupancyFraction=55"


    #This switch determines how much the objects may age in the young generation before getting promoted to the older generation.
    JAVA_OPTS="$JAVA_OPTS -XX:MaxTenuringThreshold=3"
    #   JAVA_OPTS="$JAVA_OPTS -XX:CMSIncrementalSafetyFactor=20"


    #This is workaround for JVM bugs: 6546278, 2150325, 2150326. Remove after new JVM version is installed
    JAVA_OPTS="$JAVA_OPTS -XX:+UseMembar"


# *********** parallel GC options  ***********
#    JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC"
#    JAVA_OPTS="$JAVA_OPTS -XX:InitialSurvivorRatio=5"
#    JAVA_OPTS="$JAVA_OPTS -XX:-UseAdaptiveSizePolicy"
#    JAVA_OPTS="$JAVA_OPTS -XX:-UsePSAdaptiveSurvivorSizePolicy"
#     JAVA_OPTS="$JAVA_OPTS -XX:TargetSurvivorRatio=90"




# *********** commong GC options ***********


     JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC"
     #fix for jre18u crash
     JAVA_OPTS="$JAVA_OPTS -XX:-ReduceInitialCardMarks"


CMD="java -classpath $cp $JAVA_OPTS $MAIN_CLASS $@"


$CMD 2>&1 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章