运维-java-启动脚本-找最新包启动
总体说明
如果需要nohup.log,则结尾 > nohup.log 2>&1 &
如果不需要已经定义好日志目录了,则结尾 >/dev/null & 或 >/dev/null 2>/dev/null & ,可以实测看下。
脚本1
#!/bin/bash
PID=`ps -ef| grep safety-eduinp| grep -v grep|awk '{print $2}'`
[ -n "$PID" ] && kill -9 $PID
nohup /usr/bin/java -Xms512m -Xmx512m \
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./ \
-jar /data/safety-eduinp/*.jar \
--spring.profiles.active=prd \
--spring.cloud.nacos.server-addr=10.10.94.58:8848 \
--spring.cloud.nacos.config.namespace=68dfbbc0-ec66-4663-aeca-ce02d68f3b88 \
--spring.cloud.nacos.discovery.ip=10.10.94.57 > /dev/null 2>&1 &
脚本2
kill -9 $(netstat -lnp | grep 8015|awk '{print $7}'|cut -d/ -f1) & sleep 5 & nohup java -jar *.jar >/dev/null 2>/dev/null &
kill -9 $(netstat -lnp | grep 8015|awk '{print $7}'|cut -d/ -f1) & sleep 5 & nohup java -jar *.jar >/dev/null &
-- 测试失败 ,连续启动失败,但是可以获取到端口
kill -9 $(ps -ef|grep jinlanhui.api.mp-1.0-SNAPSHOT|grep -v grep|grep -v kill|awk '{print $2}') & sleep 5 & nohup java -jar *.jar >/dev/null &
-- 这个获取最新的文件名
dest_jar=$(ls -t *.jar | head -1)
根据名字找到进程号
APP_NAME=${dest_jar: 0:9}
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
脚本3
启动脚本 restart.sh (这个应该支持重启)
#!/bin/sh
#---------------找到最新更新的文件---------------------
dest_jar=$(ls -t *.jar | head -1)
echo 'dest_jar='$dest_jar
#---------------stopping specifical jar---------------------
# 不同月份打出来的包,名字后缀不同,所以需要处理一下,通过包名关键词找到进程号
# 另外也有通过端口号找到进程号的,参考《运维-jenkins-启动脚本-maven工程jar启动springboot》
APP_NAME=${dest_jar: 0:9}
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'App is running.'
else
echo 'App is NOT running.'
fi
rm -f tpid
# ----------------starting a jar----------------
nohup java \
-Xms128m -Xmx1500m \
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./ \
-jar $dest_jar \
--spring.profiles.active=test \
--spring.cloud.nacos.server-addr=192.168.0.36:8848 \
--spring.cloud.nacos.config.namespace=5c9498d6-1357-4c13-86b5-245a727c9454 \
--spring.cloud.nacos.discovery.ip=xxx.xxx.xxx.xxx \
> /dev/null &
echo "App is started."
echo $! > tpid
花木兰短版
dest_jar=$(ls -t *.jar | head -1) && nohup java -Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./ -jar $dest_jar --spring.profiles.active=test --spring.cloud.nacos.server-addr=192.168.0.36:8848 --spring.cloud.nacos.config.namespace=5c9498d6-1357-4c13-86b5-245a727c9454 \--spring.cloud.nacos.discovery.ip=xxx.xxx.xxx.xxx > /dev/null &
停止脚本 close.sh
#!/bin/sh
#---------------finding latest jar file---------------------
dest_jar=$(ls -t *.jar | head -1)
echo 'dest_jar='$dest_jar
#---------------stopping specifical jar---------------------
APP_NAME=${dest_jar: 0:9}
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'App is running.'
else
echo 'App is NOT running.'
fi
rm -f tpid
正文到此结束