1. 使用须知
- 需要将当前脚本放到jar包所在的目录;
- 默认执行当前脚本所在目录中最新的jar(☆)
- 需要修改脚本中的
JAVA_HOME
为对应自己服务器上的路径 - 根据需要调整执行java程序的内存相关参数(在脚本中
java -jar -Xms512m ....
) - 根据需要调整执行spring的profile(在脚本中
spring.profiles.active=test
) - 下面脚本中增了端口的判断,如果不需要则把
#if [ -z "${pid}" ]; then
打开,注释它的下一行。
#!/bin/sh
export JAVA_HOME=/opt/app/jdk1.8.0_152
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
#模糊查询最新打包的jar文件名
APP_NAME=`ls -lt *.jar | head -n 1 |awk '{print $9}'`
echo -e "The current app name is:\033[35m$APP_NAME\033[0m"
#pid=`netstat -anp|grep 8085|awk '{printf $7}'|cut -d/ -f1`
#pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
#检查程序是否在运i行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
port=`netstat -ntlp | grep " $pid/"|awk '{print $4}'|awk -F '[:]' '{print $NF}' | awk '{a=a$1" "}END{print a}'`
#如果不存在返回1,存在返回0
#if [ -z "${pid}" ]; then
if [ -z "${pid}" -o -z "${port}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is already running. pid is ${pid}, port is ${port}"
else
nohup ${JAVA_HOME}/bin/java -jar -Xms512m -Xmx512m -Xmn256m -Xss228k -XX:MaxMetaspaceSize=512m ${APP_NAME} --spring.profiles.active=test >> nohup.out 2>&1 &
echo "${APP_NAME} is starting"
while true; do
is_exist
if [ $? -eq 0 ]; then
echo -e "\n${APP_NAME} is start completed. pid is ${pid}, port is ${port}"
break;
else
echo -e ".\c"
sleep 0.02
fi
done
fi
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -15 $pid
echo "${APP_NAME} is closing. pid is $pid, port is $port"
while true; do
is_exist
if [ $? -eq "1" ]; then
echo -e "\n${APP_NAME} is shutdown"
break;
else
echo -e ".\c"
sleep 0.02
fi
done
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. pid is ${pid}, port is ${port}"
else
echo "${APP_NAME} is not running."
fi
}
#重启
restart(){
stop
sleep 1
start
}
#帮助
usage(){
echo -e "参数输入错误:\033[31m$1\033[0m , 支持以下参数:"
echo -e "\033[36mstart\033[0m\t:启动程序"
echo -e "\033[36mstop\033[0m\t:停止程序"
echo -e "\033[36mrestart\033[0m\t:重启程序"
echo -e "\033[36mstatus\033[0m\t:查看程序运行状态"
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage $1
;;
esac
将上面的脚本保存为 app.sh,同时赋予可执行权限chmod u+x app.sh
2. 使用举例
-
切换到jar包所在目录
[root@wuwei app]# cd /opt/app [root@wuwei app]# ll total 419384 -rwxr--r-- 1 root root 1729 Sep 18 10:37 app.sh -rw-r--r-- 1 root root 3690 Sep 18 10:54 autorun.log -rwxr--r-- 1 root root 195 Sep 18 10:54 autorun.sh -rw-r--r-- 1 root root 39877279 Feb 24 2021 exhibition-1.0-SNAPSHOT.jar -rw-r--r-- 1 root root 39877296 Mar 3 2021 exhibition-1.1-SNAPSHOT.jar -rw-r--r-- 1 root root 39877301 Mar 3 2021 exhibition-1.2-SNAPSHOT.jar -rw-r--r-- 1 root root 39878242 Mar 7 2021 exhibition-1.3-SNAPSHOT.jar -rw-r--r-- 1 root root 39883196 Mar 11 2021 exhibition-1.4-SNAPSHOT.jar -rw-r--r-- 1 root root 39883049 Mar 12 2021 exhibition-1.5-SNAPSHOT.jar drwxr-xr-x 8 1001 1001 277 Sep 4 2020 jdk1.8.0_152 -rw------- 1 root root 155492 Sep 18 10:41 nohup.out
-
重启
[root@iccboy app]# ./app.sh restart The current app name is:exhibition-1.5-SNAPSHOT.jar exhibition-1.5-SNAPSHOT.jar is closing. pid is 184518, port is 80 443 .. exhibition-1.5-SNAPSHOT.jar is shutdown exhibition-1.5-SNAPSHOT.jar is starting ................................... exhibition-1.5-SNAPSHOT.jar is start completed. pid is 186794, port is 80
-
停止
[root@iccboy app]# ./app.sh stop The current app name is:exhibition-1.5-SNAPSHOT.jar exhibition-1.5-SNAPSHOT.jar is closing. pid is 186794, port is 80 443 .. exhibition-1.5-SNAPSHOT.jar is shutdown
-
启动
[root@iccboy app]# ./app.sh restart The current app name is:exhibition-1.5-SNAPSHOT.jar exhibition-1.5-SNAPSHOT.jar is starting ..................................... exhibition-1.5-SNAPSHOT.jar is start completed. pid is 188024, port is 80
-
状态
[root@iccboy app]# ./app.sh status The current app name is:exhibition-1.5-SNAPSHOT.jar exhibition-1.5-SNAPSHOT.jar is running. pid is 188024, port is 80 443
-
命令输入错误
[root@iccboy app]# ./app.sh xxx 当前指向程序:exhibition-1.5-SNAPSHOT.jar 参数输入错误:xxx , 支持以下参数: start :启动程序 stop :停止程序 restart :重启程序 status :查看程序运行状态
3. 程序进程崩溃自动重启
- 创建程序启动脚本,命令为autorun.sh (也存放到/opt/app/目录下)
#!/bin/sh cd /opt/app date >> autorun.log echo "开始检查" >> autorun.log ./app.sh start >> autorun.log echo "检查结束" >> autorun.log date >> autorun.log echo -e "------\n">> autorun.log
- 加入linux定时任务
a. 输入命令crontab -e
编辑定时任务文件,默认是通过 vi编辑器打开该文件
b. 在文件的最后增加一行配置。如下, 然后保存退出文件。
上面的配置表示每两分钟执行一次。*/2 * * * * /opt/app/autorun.sh
此时,自动重启生效了。
可以查看自动重启生成的日志文件,tail -f /opt/app/autorun.log
Sun Sep 19 23:18:02 CST 2021
开始检查
The current app name is:exhibition-1.5-SNAPSHOT.jar
exhibition-1.5-SNAPSHOT.jar is already running. pid=176095
检查结束
Sun Sep 19 23:18:02 CST 2021
------
Sun Sep 19 23:20:01 CST 2021
开始检查
The current app name is:exhibition-1.5-SNAPSHOT.jar
exhibition-1.5-SNAPSHOT.jar is starting
..................................
exhibition-1.5-SNAPSHOT.jar is start completed. pid is 180533, port is 80
检查结束
Sun Sep 19 23:20:08 CST 2021
------