How to call java main method in linux shell script?
linux shell脚本如何调用java main方法?
linux shell脚本调用java main方法解决方法:
#!/bin/sh # #该脚本为Linux下启动java程序的通用脚本。即可以作为开机自启动service脚本被调用, #也可以作为启动java程序的独立脚本来使用。 # #Author: tudaxia.com, Date: 2011/6/7 # #警告!!!:该脚本stop部分使用系统kill命令来强制终止指定的java程序进程。 #在杀死进程前,未作任何条件检查。在某些情况下,如程序正在进行文件或数据库写操作, #可能会造成数据丢失或数据不完整。如果必须要考虑到这类情况,则需要改写此脚本, #增加在执行kill命令前的一系列检查。 # ################################### # 以下这些注释设置可以被chkconfig命令读取 # chkconfig: - 99 50 # description: Java程序启动脚本 # processname: test # config: 如果需要的话,可以配置 ################################### # ################################### #环境变量及程序执行参数 #需要根据实际环境以及Java程序名称来修改这些参数 ################################### #JDK所在路径 JAVA_HOME="/usr/java/jdk1.8.0_102" #执行程序启动所使用的系统用户,考虑到安全,推荐不使用root帐号 #RUNNING_USER=portal #Java程序所在的目录(classes的上一级目录) APP_HOME=/opt/tmp/geecuser/geec_calculate #需要启动的Java主程序(main方法类) APP_MAINCLASS=com.ai.core.start.Main #拼凑完整的classpath参数,包括指定lib目录下所有的jar CLASSPATH=$APP_HOME/classes for i in "$APP_HOME"/lib/*.jar; do CLASSPATH="$CLASSPATH":"$i" done #java虚拟机启动参数 JAVA_OPTS="-ms1024m -mx1024m -Xmn512m -Djava.awt.headless=true -XX:MaxPermSize=256m" ################################### #(函数)判断程序是否已启动 # #说明: #使用JDK自带的JPS命令及grep命令组合,准确查找pid #jps 加 l 参数,表示显示java的完整包路径 #使用awk,分割出pid ($1部分),及Java程序名称($2部分) ################################### #初始化psid变量(全局) psid=0 checkpid() { javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAINCLASS` if [ -n "$javaps" ]; then psid=`echo $javaps | awk '{print $1}'` else psid=0 fi } ################################### #(函数)启动程序 # #说明: #1. 首先调用checkpid函数,刷新$psid全局变量 #2. 如果程序已经启动($psid不等于0),则提示程序已启动 #3. 如果程序没有被启动,则执行启动命令行 #4. 启动命令执行后,再次调用checkpid函数 #5. 如果步骤4的结果能够确认程序的pid,则打印[OK],否则打印[Failed] #注意:echo -n 表示打印字符后,不换行 #注意: "nohup 某命令 >/dev/null 2>&1 &" 的用法 ################################### start() { checkpid if [ $psid -ne 0 ]; then echo "================================" echo "warn: $APP_MAINCLASS already started! (pid=$psid)" echo "================================" else echo -n "Starting $APP_MAINCLASS ..." JAVA_CMD="nohup $JAVA_HOME/bin/java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS >$APP_HOME/log/nohup 2>&1 &" eval $JAVA_CMD checkpid if [ $psid -ne 0 ]; then echo "(pid=$psid) [OK]" else echo "[Failed]" fi fi } ################################### #(函数)停止程序 # #说明: #1. 首先调用checkpid函数,刷新$psid全局变量 #2. 如果程序已经启动($psid不等于0),则开始执行停止,否则,提示程序未运行 #3. 使用kill -9 pid命令进行强制杀死进程 #4. 执行kill命令行紧接其后,马上查看上一句命令的返回值: $? #5. 如果步骤4的结果$?等于0,则打印[OK],否则打印[Failed] #6. 为了防止java程序被启动多次,这里增加反复检查进程,反复杀死的处理(递归调用stop)。 #注意:echo -n 表示打印字符后,不换行 #注意: 在shell编程中,"$?" 表示上一句命令或者一个函数的返回值 ################################### stop() { checkpid if [ $psid -ne 0 ]; then echo -n "Stopping $APP_MAINCLASS ...(pid=$psid) " kill -9 $psid if [ $? -eq 0 ]; then echo "[OK]" else echo "[Failed]" fi checkpid if [ $psid -ne 0 ]; then stop fi else echo "================================" echo "warn: $APP_MAINCLASS is not running" echo "================================" fi } ################################### #(函数)检查程序运行状态 # #说明: #1. 首先调用checkpid函数,刷新$psid全局变量 #2. 如果程序已经启动($psid不等于0),则提示正在运行并表示出pid #3. 否则,提示程序未运行 ################################### status() { checkpid if [ $psid -ne 0 ]; then echo "$APP_MAINCLASS is running! (pid=$psid)" else echo "$APP_MAINCLASS is not running" fi } ################################### #(函数)打印系统环境参数 ################################### info() { echo "System Information:" echo "****************************" echo `head -n 1 /etc/issue` echo `uname -a` echo echo "JAVA_HOME=$JAVA_HOME" echo `$JAVA_HOME/bin/java -version` echo echo "APP_HOME=$APP_HOME" echo "APP_MAINCLASS=$APP_MAINCLASS" echo "****************************" } ################################### #读取脚本的第一个参数($1),进行判断 #参数取值范围:{start|stop|restart|status|info} #如参数不在指定范围之内,则打印帮助信息 ################################### case "$1" in 'start') start ;; 'stop') stop ;; 'restart') stop start ;; 'status') status ;; 'info') info ;; *) echo "Usage: $0 {start|stop|restart|status|info}" exit 1 esac exit 0
推荐教程:《JAVA视频教程》
The above is the detailed content of How to call java main method in linux shell script?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
