Detailed SpringCloud startup scripts, Linux and in nohup & Detailed

1 SpringCloud script Comments

1.1 Script Case

#!/bin/bash
cd `dirname $0` 
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
#此处的项目名字 即 maven打包后包名: springcloud-eureka-server-0.0.1-SNAPSHOT.jar
JAR_NAME="项目名称"
JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME 
#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
JAVA_MEM_OPTS="" 
# 要激活的哪个服务:eureka,eureka2等之类的
SPRING_PROFILES_ACTIV="-Dspring.profiles.active=配置文件变量名称"
#SPRING_PROFILES_ACTIV=""
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME%.*}.log 
echo_help(){
    echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
}
 
if [ -z $1 ];then
    echo_help
    exit 1
fi
 
if [ ! -d "$LOG_DIR" ];then
    mkdir "$LOG_DIR"
fi
 
if [ ! -f "$LOG_PATH" ];then
    touch "$LOG_DIR"
fi
 
 if [ "$1" == "start" ];then
     # check server
    PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
    if [ -n "$PIDS" ]; then
        echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
        exit 1
    fi
     echo "Starting the $JAR_NAME..."
 
    # start
    nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &
 
    COUNT=0
    while [ $COUNT -lt 1 ]; do
        sleep 1
        COUNT=`ps  --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
        if [ $COUNT -gt 0 ]; then
            break
        fi
    done
    PIDS=`ps  --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
    echo "${JAR_NAME} Started and the PID is ${PIDS}."
    echo "You can check the log file in ${LOG_PATH} for details."
 
elif [ "$1" == "stop" ];then
 
    PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
    if [ -z "$PIDS" ]; then
        echo "ERROR:The $JAR_NAME does not started!"
        exit 1
    fi
 
    echo -e "Stopping the $JAR_NAME..."
 
    for PID in $PIDS; do
        kill $PID > /dev/null 2>&1
    done
 
    COUNT=0
    while [ $COUNT -lt 1 ]; do
        sleep 1
        COUNT=1
        for PID in $PIDS ; do
            PID_EXIST=`ps --no-heading -p $PID`
            if [ -n "$PID_EXIST" ]; then
                COUNT=0
                break
            fi
        done
    done
 
    echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
else
    echo_help
    exit 1
fi

1.2 script to explain

1.2.1 dirname $0

dirname $0This command written 脚本文件in the only role, it returns the script files are placed in the directory, and can locate the program you want to run according to this directory 相对位置(except for absolute position)

1.2.2 basename

basename:file name

1.2.3 ${JAR_NAME%.*}.log

Click to view the role of the special symbol of linux blog

1.2.4 ps --no-heading row at grammar

psThis is the command syntax condition reporting procedures

  • --no-headingThis effect parameter and the specified "h" of the same parameters (not displayed title bar), only a slight difference in the list of formats
  • -C javaCondition specifying program name executed instructions, and lists the instruction corresponds here filtered javainformation
  • -fDisplay UID,PPIP,C与STIMEfield
  • --width 1000Sets the maximum number of characters per column
  • awk '{print $2}'
    $2: Represents the second field
    print $2: Print the second field
    awk '{print $2}' $fileName: reading the specified file line by line, a space as a separator, a second print field
    , such as a file such
a1  b1  c1  d1
a2  b2  c2  d2

The results of implementation, the output

b1
b2

1.2.5 nohup at grammar

1.2.5.1nohup knowledge to understand

nohupRun the command Commandparameters and any associated Argparameters specified command, ignore all hang ( SIGHUP) signal.
Used after the cancellation of nohupthe program in the background to run the command. To run a background in the nohupcommand, add &(represented andsymbols) to 命令尾部.
nohupIs no hang upan abbreviation, is the 不挂断meaning.
nohupCommand: If you are running a process, and you feel that the process will not end when you exit your account, you can use nohupthe command. This command can you 退出帐户/关闭continue to run the process after the corresponding terminal.
All default output the job are redirected to a named nohup.outfile.

1.2.5.2 nohup Case

1.2.5.2.1 Case One
nohup command > myout.file 2>&1 &   

In the example above:

  • 0stdin(standard input)标准输入
  • 1stdout (standard output)标准输出
  • 2stderr (standard error) 标准错误

command >out.fileIt is to commandredirect the output to a myout.filefile, that is 输出内容not to print 屏幕on, 2>&1is 标准错误(2)redirected to 标准输出(&1), but here the standard output has been redirected to a myout.filefile, and therefore will 标准出错also be output to a myout.filefile, the last & is to make it in the background carried out

1.2.5.2.2 Case II
> /dev/null 2>&1

/dev/nullIt can be seen 黑洞. It is equivalent to one 只写文件and all its contents will be written 永远丢失while trying to read content from there it did not read it.
/dev/null 2>&1It means to 标准输出and 错误输出put into this "black hole" and what does not indicate output

1.2.5.3 nohup and & differences

  • &: Refers to 后台运行, but when the user exits ( 挂起) when the command automatically also pulled out
  • nohup: , 不挂断的运行And pay attention to 没有后台运行the function refers to using nohupthe Run command allows permanent command execution continues, and the user terminal does not matter, for example, we disconnect the SSHconnection will not affect its operation, attention nohupis not running in the background meaning; &is Background process

1.3 script error

1.3.1 start error

linuxError on startup script: -bash: ./bin/start.sh: /bin/bash^M: bad interpreter: No such file or directory
原因is the format of the file is dosamended as unixto OKthe
specific steps:

  1. Check the file format used vimto open the files with errors ESCbutton and then shift + :enter set ffa carriage return you can see the format of the filefileformat=dos
  2. Press shift + :Enter set ff=unixEnter discovery did not respond, you are right.
  3. You can press shift + :the line low to set ffViewfileformat=unix
Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/104317873