Hello everyone, I am Xiongxiong. My WeChat public account is: Xiongxiong’s Little Classroom. Welcome to follow me.
Preface
We all know that springboot
when starting a project, the following process is required:
- Find the process id of a service
- Kill the process
- Start service
And each step has corresponding shell
commands, which are as follows:
ps aux|grep blog-mxx.jar
kill -9 10000
nohup java -jar blog-mxx.jar --server.port=8800 > mxxblog.log 2>&1 &
So, here comes the question. If we need to do this every time we release a version, it will be very tiring... Is there a command that can directly execute the file sh
to complete the above steps?
Of course there is.
Automated service restart command
#!/bin/bash
#重启脚本
# 查找并杀死进程
pid=$(ps -ef | grep blog-mxx.jar | grep -v grep | awk '{print $2}')
if [ -n "$pid" ]; then
kill $pid
fi
# 启动服务
nohup java -jar blog-mxx.jar --server.port=8800 > mxxblog.log 2>&1 &
For the above code, we put it directly sh
in a file, then pass it to the project and linux
execute it directly in the system sh start.sh
. (Note that the file saved by the above code is named start.sh
)
Problems encountered
An error may be reported:
start.sh: line 11: syntax error: unexpected end of file
This is also easy to solve, it seems to be a file format issue. Solved like this:
- Use
vim
the command to openstart,sh
the file, then look at the file format and enter the following command:
vim start.sh
:set ff
If the output is fileformat=doc
, it proves to be a format problem. Enter the following command:
:set ff = unix
Then execute the saved command:
:wq
You can execute the query command:
:set ff
See if the format has changed fileformat=unix
. If so, you can execute start.sh
the file.
sh start.sh
That’s it for a long time. If you are still worried, you can check the input log:
tail -f mxxnlog.log