The Java program runs in the background in Linux, and the java program runs in the background in linux.

Source: Internet
Author: User

The Java program runs in the background in Linux, and the java program runs in the background in linux.
1. Run the script

The directory structure of the program is as follows:

[root@bogon SocketDxpTaxi]# lsconfig  lib  logs  run.sh  SocketDxpTaxi.jar[root@bogon SocketDxpTaxi]# find../run.sh./lib./lib/spring-data-commons-1.9.0.RELEASE.jar./lib/aspectjrt-1.8.2.jar......./lib/mina-integration-beans-2.0.9.jar./lib/jandex-1.1.0.Final.jar./SocketDxpTaxi.jar./logs./logs/socket.log./config./config/applicationContext.xml./config/jdbc.properties./config/log4j.properties

SocketDxpTaxi. jar is the jar to be run, config is the configuration file of the program, lib is the jar that the program depends on, and logs stores the log files, which must be the same level.
The program structure in eclipse is as follows:
Src
-|-Com. tiamaes .*
-|-Config
Use eclipse to export the program as jar. Generally, the jar contains config. In this case, you need to delete the config folder in the jar. Otherwise, the file conflict occurs.

The script for running jar is as follows. This script has some problems. Log4j is slightly faulty, but it does not affect the running and log records. logs are recorded by writing all the console data into files.

#! /Bin/sh # author: wangchengwei # date: 2015-7-7 # desc: run java application # Java installation directory JAVA_HOME =/usr/lib/java-1.7.0 # user OWNER used to Run the program = root # APP_HOME directory of the Java program =/software/TaxiData/ socketDxpTaxi # Main method class APP_MAINCLASS = com. tiamaes. gjds. socket. server. minaServer # Log File LOG_FILE = $ APP_HOME/logs/socket. log # Set CLASSPATHCLASSPATH = $ CLASSPATH: $ APP_HOME/SocketDxpTaxi. jar # cyclically add all the jar files in the lib folder to CLASSPATHfor I in "$ APP_HOME"/lib /*. jar; Do CLASSPATH = "$ CLASSPATH": "$ I" done # Set the runtime parameter JAVA_OPTS = "-Xms512m-Xmx512m-Xmn256m-Djava. awt. headless = true-XX: MaxPermSize = 128 m "# echo $ CLASSPATH; psid = 0 # Check whether the Java program runs checkpid () {javaps = '$ JAVA_HOME/bin/jps-l | grep $ APP_MAINCLASS' if [-n "$ javaps"]; then psid = 'echo $ javaps | awk '{print $1} ''else psid = 0 fi} # run the program start () {checkpid if [$ psid-ne 0]; then echo "WARN: $ APP_MAINCLASS already started! (Pid = $ psid) "else echo" Starting $ APP_MAINCLASS... "JAVA_CMD =" nohup $ JAVA_HOME/bin/java $ JAVA_OPTS-classpath $ CLASSPATH $ APP_MAINCLASS >>$ LOG_FILE 2> & 1 & "su-$ OWNER-c" $ JAVA_CMD "checkpid if [$ psid-ne 0]; then echo "Started $ APP_MAINCLASS (pid = $ psid) [OK]" else echo "Started $ APP_MAINCLASS [FAILED]" fi} # stop the program stop () {checkpid if [$ psid-ne 0]; then echo "Stoping $ APP_MAINCLASS... (pid = $ psid) "Su-$ OWNER-c" kill $ psid "checkpid if [$ psid-ne 0]; then echo "Stoping use kill-9" su-$ OWNER-c "kill-9 $ psid" fi checkpid if [$ psid-eq 0]; then echo "Stoped $ APP_MAINCLASS [OK]" else echo "Stoped $ APP_MAINCLASS [Failed]" stop fi else echo "WARN: $ APP_MAINCLASS is not runing "fi} # view status () {checkpid if [$ psid-ne 0]; then echo" $ APP_MAINCLASS is runing (pid = $ psid) "else echo" $ APP_M AINCLASS is not runing "fi} # Help info () {echo" System Information: "echo" *************************** "echo 'head-n 1/etc /issue 'echo 'uname-a' echo "JAVA_HOME = $ JAVA_HOME" echo '$ JAVA_HOME/bin/java-version' echo "APP_HOME = $ APP_HOME" echo "APP_MAINCLASS = $ APP_MAINCLASS "echo" **************************** "} #$1 indicates receiving the first parameter, for example. /run. sh start. Then $1 is start case "$1" in 'start') start; 'stop') stop; 'restart') stop start; 'info') info ;; 'status') status; *) echo "Usage: $0 {start | stop | restart | status | info}" exit 1 esacexit 0;
2. Knowledge Point Analysis

I will not repeat the basic knowledge of shell. I will learn it from Baidu. Focus on the following.

2.1 run jar
JAVA_OPTS="-Xms512m -Xmx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"# java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS

You can set Java runtime parameters, classpath, and other options when running jar.

Java runtime parameters mainly include memory settings
-Xms: the initial Heap size, the minimum memory used, and the value should be larger when the cpu performance is high.
-Xmx: Maximum java heap memory usage
-XX: PermSize: Set the permanent storage area of the memory.
-XX: MaxPermSize: Specifies the permanent storage area of the maximum memory.
-Xmn: the heap size of young generation, which is usually set to one of the 3 and 4 points of Xmx.

Classpath is used to set jar to be dependent on

2.2. jps commands in Java

Jps (Java Virtual Machine Process Status Tool) is a command provided by JDK 1.5 to display the current pid of all java processes. It is simple and practical, it is very suitable for viewing some simple information about the current java Process on linux/unix platforms.
Common parameters:
-Q: Only pid is displayed, and class name is not displayed. jar file name and parameters passed to main method are also displayed.
-M output: The parameter passed to the main method. It may be null on the embedded jvm.
-M output: The parameter passed to the main method. It may be null on the embedded jvm.
-L output the complete package name of the main class of the application or the full path name of the jar file of the application
-V output parameters passed to JVM
In this example, we use jps-l and use jps-l | grep $ APP_MAINCLASS to check whether our program exists in the process based on the class name of the Main method. Then obtain the process pid through awk.

2.3 use of the su command
su - $OWNER -c "$JAVA_CMD"

The above command indicates that the $ OWNER user is used to execute the command, followed by the command-c. The advantage of this is that the execution carries the user's environment variables and ensures that our applications run under the user we specify.

2.4. nohup

Purpose: run the command without hanging up. Simply put, the program can be run in the background.
Syntax: nohup Command [Arg... ] [&]

Description: The nohup Command runs the Command specified by the Command parameter and any relevant Arg parameter, ignoring all SIGHUP signals. After logging out, run the nohup command to run the program in the background. To run the nohup command in the background, add the & (symbol indicating "and") to the end of the command.

Whether or not the output of the nohup command is redirected to the terminal, the output will be appended to the nohup. out file in the current directory. If the nohup. out file in the current directory cannot be written, the output will be redirected to the $ HOME/nohup. out file. If no file can be created or opened for append, the Command specified by the Command Parameter cannot be called. If the standard error is a terminal, write the specified command to all outputs of the standard error as the standard output and redirect it to the same file descriptor.

This means that our Java program runs in the background and records the logs generated by the program to $ LOG_FILE.> indicates writing (clearing the content before writing the content),> indicates appending

2> & 1

For & 1, it should be file descriptor 1, while 1 generally represents STDOUT_FILENO. In fact, this operation is a dup2 (2) Call. it outputs the standard output to all_result, and then copies the standard output to file descriptor 2 (STDERR_FILENO). The consequence is that file descriptors 1 and 2 point to the same file table item, it can also be said that the wrong output is merged. 0 indicates keyboard input 1 indicates screen output 2 indicates wrong output. redirects a standard error to the standard output and then throws it to/DEV/NULL. In layman's terms, all the standard output and standard errors are thrown into the waste bin.

2> & 1 redirects a standard error to the standard output. The standard output is redirected to the $ LOG_FILE file, which outputs a standard error to the $ LOG_FILE file.

For more information, see this blog: http://blog.csdn.net/annicybc/article/details/4814872

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.