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服务器







































































































































































































执行的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 


你可能感兴趣的:(linux下操作)