Prevent the same Java application from repeatedly starting shell scripts

Source: Internet
Author: User

In general, a Java background application, in the Linux terminal can start multiple, which causes waste of resources, and even the mutual operation of competing resources cause problems. The application is also launched using shell script, and there are some limitations in the shell script. You can skillfully avoid the problem of repetitive startup.

For example, the application directory structure is:

app
        bin(存放启动脚本startup.sh和class文件、配置文件等。)
        lib(存放引用的库)

Suppose the application class name is: Mypack. Myappmain

startup.sh

#!/bin/sh
programdir= "."
program= "Mypack. Myappmain the
num=$#
temp= $CLASSPATH
#setting libs path
libs= ...    /lib/*
Append () {
temp= $temp ":"
}
for file in $libs; Do
Append $file
Done
Export classpath= $temp:.:.. /: $programdir
Export LANG=ZH_CN
res= ' ps aux|grep java|grep $program |grep-v Grep|awk ' {print $} '
If [-N ' $r Es "]
then
Echo" Myappmain already running "
Else

Nohup java- Classpath $CLASSPATH Mypack. Myappmain

Sleep 3
unset res
res= ' PS aux|grep java|g                           Rep $program |grep-v Grep|awk ' {print $} '
If [-N ' $res]
then
echo "Myappmain start Success"
Else
Echo MyApp Main start Error "
Fi
Fi
"/p>

You can then use this script to start up and solve the problem.

Attention:

There may also be a problem with startup.sh not executing permissions at startup, to 777.

You may also receive an error message:

: bad interpreter: 没有那个文件或目录

This is because the startup.sh script encoding is not correct, you need to ensure that the document format is UNIX, this problem many people have stumbled, I am no exception, that the shell script syntax is not correct, in fact, is the problem of file encoding!

The following is a script that does not have a limited recurring startup problem:

#!/bin/sh
programdir="."
num=$#
temp=$CLASSPATH
#setting libs path
libs=../lib/*
append(){
                 temp=$temp":"$1
}
for file in $libs;    do
                 append $file
done
export CLASSPATH=$temp:.:../:$programdir
export LANG=zh_CN
nohup java    -classpath $CLASSPATH    mypack.MyAppMain &

Source: http://lavasoft.blog.51cto.com/62575/243360

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.