Dynamically deploy springboot projects on Linux servers

16467414:

1. Write the script (the code is as follows)

#一些常量
PORT=8888
APP_NAME=blog.jar
CUR_PATH=$pwd
#配置文件所在目录
APPFILE_PATH="/usr/local/java"
 
 
#第一步先杀死占用后端端口的进程,一般后端端口是8011,对应下句port的值
echo "端口号为${PORT}"
echo "启动jar包名称${APP_NAME}"
echo "启动资源路径${APPFILE_PATH}"
#根据端口号查找出响应进程id
pid=$(netstat -nlp | grep :$PORT | awk '{print $7}' | awk -F "/" '{print $1}')
echo "端口号对应进程id,pid=${pid}"
#杀掉pid对应的进程
if [ -n "$pid" ]; then
    kill -9 $pid;
fi
echo "杀死pid对应进程"
#运行项目,打印日志
nohup java -jar $APP_NAME  > nohup.out &
tail -f nohup.out
echo "--------------------------------"

2. Upload the script to the corresponding directory

3. Start and run the script

cd 到相应目录下(cd /usr/local/java)
./blogStart.sh

problem and solution

You may encounter the following similar problems, after executing ./blogStart.sh
-bash: ./blogStart.sh: Permission denied

solve:

This is caused by permission issues

chmod u+x start.sh
Usage:
chmod [who] [opt] [mode] The file/directory name
who represents the object, which is one or a combination of the following letters:

u: User, the owner of the file or folder.
g: Group, the group to which the file or folder belongs.
o: Other, except for the file or directory owner or group, other users belong to this scope.
a: All, that is, all users, including the owner, the group they belong to, and other users.

opt represents operation, which can be:
+: add a certain permission
-: cancel a certain permission
=: grant a given permission, and cancel the original permission

mode represents permission:
r: readable
w: writable
x: executable

reason:

The .sh script file formats of Windows and Linux are different. If there are blank lines in the script, and the script is edited under Windows and then uploaded to Linux for execution, this problem will occur.
The newline character under windows is \r\n, while the newline character under linux is \n, and /r is not recognized, so it will cause the above error report, which is a script encoding problem.
The shell script is edited on the local computer, and the format is dos (you can use vi to edit the shell script, enter the command line mode, enter: set ff and press Enter to see fileformat=dos in the lower left corner), press once under dos/window The enter key actually enters "carriage return (CR)" and "line feed (LF)", while pressing the enter key once under Linux/unix only enters "line feed (LF)", so the locally modified sh file is in CentOS There will be one more CR running on each line, so it will report an error syntax error: unexpected end of file.

solve:

The solution is very simple, open the shell script with vi editor, enter the command line mode (CTRL+C), enter: set ff=unix, then wq save and exit. At this time, if you check through set ff, you will find fileformat=unix.

Guess you like

Origin blog.csdn.net/weixin_44030860/article/details/129735968