一.背景
通常呢,我们springboot项目部署测试重启的时候,先查看进程,然后杀进程,然后再启动,麻烦的很。有没有一步到位的命令呢,那是当然的。
假设我们的项目打包后是: springboot-demo.jar
一般的做法:
1)首先查看进程 ps -ef | grep springboot-demo
[root@iz2ze25urefxhwyo31qj2oz scripts]# ps -ef | grep springboot-demo
root 4745 1 0 Aug12 ? 04:04:48 java -jar -Dlogging.file=/data/test/logs/test.log springboot-demo.jar
2) 杀进程 kill PID
[root@iz2ze25urefxhwyo31qj2oz scripts]# kill 4745
3) 重新启动
[root@iz2ze25urefxhwyo31qj2oz scripts]# nohup java -jar -Dlogging.file=/data/test/logs/test.log springboot-demo.jar &
看上去是不是很麻烦,如果更新比较频繁的话,会不会“疯”?所以要用到一键启动脚本:
二.一键启动脚本
废话少说:上脚本:这里我命名为:springboot_script.sh
#!/bin/bash
#这里可替换为你自己的程序,其他代码无需更改
APP_NAME=springboot-demo.jar
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh scriptName.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -jar -Dlogging.file=/data/test/logs/test.log springboot-demo.jar
echo "${APP_NAME} start success"
fi
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart(){
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
使用方法:
1) 将第3行APP_NAME=springboot-demo.jar 设置为自己的jar名称
2)将启动方法(start)28行的nohup java -jar -Dlogging.file=/data/test/logs/test.log springboot-demo.jar改成自己的日志文件和jar名称,当然也可以加一些其他的java的启动参数。
3)执行如下命令即可重启程序:
sh springboot_script.sh restart
三.填坑
报错: $’\r’:command not found
解决方法:
[root@iz2ze25urefxhwyo31qj2oz scripts]# dos2unix merapi_restart_new.sh
-bash: dos2unix: command not found
[root@iz2ze25urefxhwyo31qj2oz scripts]# yum install dos2unix -y
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 5.3 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(2/4): extras/7/x86_64/primary_db | 215 kB 00:00:00
(3/4): epel/x86_64/primary_db | 6.8 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 7.4 MB 00:00:00
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package dos2unix.x86_64 0:6.0.3-7.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================
Package Arch Version Repository Size
=================
Installing:
dos2unix x86_64 6.0.3-7.el7 base 74 k
Transaction Summary
=================
Install 1 Package
Total download size: 74 k
Installed size: 190 k
Downloading packages:
dos2unix-6.0.3-7.el7.x86_64.rpm | 74 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : dos2unix-6.0.3-7.el7.x86_64 1/1
Verifying : dos2unix-6.0.3-7.el7.x86_64 1/1
Installed:
dos2unix.x86_64 0:6.0.3-7.el7
Complete!
[root@iz2ze25urefxhwyo31qj2oz scripts]#
[root@iz2ze25urefxhwyo31qj2oz scripts]# dos2unix merapi_restart_new.sh
dos2unix: converting file merapi_restart_new.sh to Unix format ...
[root@iz2ze25urefxhwyo31qj2oz scripts]#
转换完成后,就可以正常启动了。完美!