SpringBoot runs methods in the linux background with source code

1. On the linux server, add the &symbol at the end of the program as follows:

java -jar test.jar &

&Symbol, in the linux system, means to let the process execute in the background of the system, but once the process has output, your console will be occupied by the information currently output.

 

2. Consider redirecting the standard output to a specified file using the > directive provided by linux. Two variables (1, 2) are defined by default in the > directive of linux. One represents the standard output, and two represents the error output. The directive then evolves as follows:

java -jar test.jar 1> output_common_log_file_path 2> output_error_log_file_path &

Since standard output is used by default, it is also equivalent to the following

java -jar test.jar > output_common_log_file_path 2> output_error_log_file_path &

 

3. It is possible to only want some output at this time, either by using device/dev/null to ignore the output.

3.1 Ignore standard output

java -jar test.jar 1> /dev/null 2> output_error_log_file_path &

3.2 Ignore error output

java -jar test.jar 1> output_common_log_file_path 2> /dev/null &

3.2 Ignore all output

java -jar test.jar 1> /dev/null 2> /dev/null &

 

4. Information redirection, assigning all outputs to the same file

4.1 redirects the output of 2 to 1

java -jar test.jar 1> output_common_log_file_path 2> &1 &

4.2 Ignore all output

java -jar test.jar 1> /dev/null 2> &1 &

Error demonstration: Although it is possible to redirect to the same file, it cannot be output directly to the same file, otherwise it will conflict as follows

java -jar test.jar 1> output_log_file_path 2> output_log_file_path &

 

5. Every time you run a command, the original file is overwritten, not appended. If you want to append, you need to use the >> command

java -jar test.jar 1>> output_common_log_file_path 2>> output_error_log_file_path &

 

6. Background processes initiated by the &symbol are fine when the shell is open all the time. If the shell window is closed or even the ssh login or vnc login is exited, the process will end automatically. The nohup directive of linux is needed.

nohup java -jar test.jar 1> output_common_log_file_path 2> output_error_log_file_path &

 

7. After sorting out the sh file, the current output is not > log.

#!/bin/sh
APP_DIR=/app/server/cuohe-admin
APP_FILE=yaycrawler-admin-1.0.0-SNAPSHOT.war
#APP_CONF=$APP_DIR/application.properties

#set java home
#export JAVA_HOME=/opt/jdk1.8.0_111
usage(){
    echo 'Usage: sh yaycrawler-admin-1.0.0-SNAPSHOT.sh [start|stop|deploy|check]'
    exit 1
}

kills(){
    tpid=`ps -ef|grep $APP_FILE|grep -v grep|grep -v kill|awk '{print $2}'`
    if [[ $tpid ]]; then
	echo 'Kill Process!'
	kill -9 $tpid
    fi
}

start(){
    rm -f $APP_DIR/tpid

    if [[ $APP_CONF ]]; then
        nohup java -jar $APP_DIR/"$APP_FILE" --spring.config.location="$APP_CONF" >> /dev/null 2>>&1 &
	echo 'starting...'
    else
	nohup java -jar $APP_DIR/"$APP_FILE" >> /dev/null 2>>&1 &
	echo 'starting...'
    fi

    echo $! > $APP_DIR/tpid
    echo 'Start Success!'
}

stop(){
    tpid1=`ps -ef|grep $APP_FILE|grep -v grep|grep -v kill|awk '{print $2}'`
    echo "tpid1-$tpid1"
    if [[ $tpid1 ]]; then
	echo 'Stop Process...'
	kill -15 $tpid1
    fi
    sleep 5
    tpid2=`ps -ef|grep $APP_FILE|grep -v grep|grep -v kill|awk '{print $2}'`
   
    if [[ $tpid2 ]]; then
        echo "tpid2-$tpid2"
	echo 'Kill Process!'
	kill -9 $tpid2
    else
	echo 'Stop Success!'
    fi
}

check(){
    tpid=`ps -ef|grep $APP_FILE|grep -v grep|grep -v kill|awk '{print $2}'`
    if [[ $tpid ]]; then
	echo 'App is running.'
    else
	echo 'App is NOT running.'
    fi
}

deploy(){
    kills
    rm -rf $APP_DIR/"$APP_FILE"
    cp "$APP_FILE" $APP_DIR
}

case "$1" in
    "start")
	start;;
    "stop")
	stop;;
    "kill")
	kills;;
    "deploy")
	deploy;;
    "check")
	check;;
    *)
	usage;;
esac

Run as,. /sh file [start|stop|deploy|check] these four instructions.

If started:. /yaycrawler-admin-1.0.0-SNAPSHOT.sh start

 

8. Several problems you may encounter when running sh files

8.1 \302\240,command not found

When you see this error, you know that you are a sh script written in Windows environment, edit the current sh file in vim environment, find line 48 in the figure, delete the space, and re-fill the space, because the character encoding of windows and linux is different.

8.2 syntax error: unexpected end of file

This error means the same thing as above. Write it in vim to solve this problem.

Keywords: Java Linux snapshot shell

Added by dbradbury on Fri, 28 Jun 2019 19:27:31 +0300