Article Directory
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 $0
This 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
ps
This is the command syntax condition reporting procedures
--no-heading
This effect parameter and the specified "h" of the same parameters (not displayed title bar), only a slight difference in the list of formats-C java
Condition specifying program name executed instructions, and lists the instruction corresponds here filteredjava
information-f
DisplayUID,PPIP,C与STIME
field--width 1000
Sets the maximum number of characters per columnawk '{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
nohup
Run the command Command
parameters and any associated Arg
parameters specified command, ignore all hang ( SIGHUP
) signal.
Used after the cancellation of nohup
the program in the background to run the command. To run a background in the nohup
command, add &
(represented and
symbols) to 命令尾部
.
nohup
Is no hang up
an abbreviation, is the 不挂断
meaning.
nohup
Command: If you are running a process, and you feel that the process will not end when you exit your account, you can use nohup
the 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.out
file.
1.2.5.2 nohup Case
1.2.5.2.1 Case One
nohup command > myout.file 2>&1 &
In the example above:
0
–stdin
(standard input)标准输入
1
–stdout
(standard output)标准输出
2
–stderr
(standard error)标准错误
command >out.file
It is to command
redirect the output to a myout.file
file, that is 输出内容
not to print 屏幕
on, 2>&1
is 标准错误(2)
redirected to 标准输出(&1)
, but here the standard output has been redirected to a myout.file
file, and therefore will 标准出错
also be output to a myout.file
file, the last & is to make it in the background carried out
1.2.5.2.2 Case II
> /dev/null 2>&1
/dev/null
It 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>&1
It 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 outnohup
: ,不挂断的运行
And pay attention to没有后台运行
the function refers to usingnohup
the Run command allows permanent command execution continues, and the user terminal does not matter, for example, we disconnect theSSH
connection will not affect its operation, attentionnohup
is not running in the background meaning;&
is Background process
1.3 script error
1.3.1 start error
linux
Error on startup script: -bash: ./bin/start.sh: /bin/bash^M: bad interpreter: No such file or directory
原因
is the format of the file is dos
amended as unix
to OK
the
specific steps:
- Check the file format used
vim
to open the files with errorsESC
button and thenshift + :
enterset ff
a carriage return you can see the format of the filefileformat=dos
- Press
shift + :
Enterset ff=unix
Enter discovery did not respond, you are right. - You can press
shift + :
the line low toset ff
Viewfileformat=unix