1:在线上启动脚本添加如下的内容:
#! /bin/sh
# 设置启动的jar
SERVICE_NAME="xxx.jar"
PRJ_BIN_DIR=$(dirname $(readlink -f "$0"))
SERVICE_HOME=$(dirname $PRJ_BIN_DIR)
LOGS_DIR=$SERVICE_HOME/logs
# 控制台日志
STDOUT_FILE=$SERVICE_HOME/logs/stdout.out
# 判断该服务名称是否已经启动加载了
PIDS=$(ps --no-heading -C java -f --width 1000 | grep $SERVICE_NAME |awk '{print $2}')
if [ -n "$PIDS" ]; then
echo "ERROR: The $SERVICE_NAME already started!"
echo "PID: $PIDS"
exit 1
fi
# java 常规启动
JAVA_OPTS="-Dproject.dir=$SERVICE_HOME"
# 默认启动内存配置
JAVA_MEM_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=19052 -server -Xmx1048m -Xms1048m -Xmn512m -XX:PermSize=256m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Dfile.encoding=UTF-8 -Dlog4j2.formatMsgNoLookups=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/a/b/c/oom -XX:+PrintGCDetails -XX:+PrintGCDateStamps"
# 整合JAVA启动参数
JAVA_EXEC="$JAVA_OPTS $JAVA_MEM_OPTS -jar $SERVICE_HOME/$SERVICE_NAME --EUREKA_SERVER=http://x.x.x.x:5200/eureka --config_uri=http://x.x.x.x:5100/"
#建立日志目录
mkdir -p $LOGS_DIR
# 正式启动区
# =====================================
echo -e "Starting the $SERVICE_NAME ...\c"
nohup java $JAVA_EXEC > $STDOUT_FILE 2>&1 &
# 启动后检测
# =====================================
COUNT=0
while [ $COUNT -lt 1 ]; do
echo -e ".\c"
sleep 1
COUNT=$(ps --no-heading -C java -f --width 1000 | grep "$SERVICE_NAME" | awk '{print $2}' | wc -l)
if [ $COUNT -gt 0 ]; then
break
fi
done
echo "OK!"
PIDS=$(ps --no-heading -C java -f --width 1000 | grep "$SERVICE_NAME" | awk '{print $2}')
echo "STDOUT: $STDOUT_FILE"
echo -e "Start success!"
echo "RUN PID: $PIDS"
ps:远程添加的信息如图
2:停止脚本如下:
#!/bin/bash
#
# 服务停止shell脚本
# 这里的服务是指不需要web等容器加载的,直接java命令关闭
#
#
SERVICE_BIN_DIR=$(dirname $(readlink -f "$0"))
SERVICE_HOME=$(dirname $SERVICE_BIN_DIR)
#启动服务的服务名和端口号,一般建议配在配置文件当中,启动和关闭时方便处理
#服务名称和部署目录名称一般保持一致吧
SERVICE_NAME=$(basename $SERVICE_HOME)
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=$(hostname)
fi
echo "begin stopping $SERVICE_NAME"
#找到服务对应的pid来停掉,根据服务名或者端口号来找都可以吧
PID=`ps -ef|grep $SERVICE_NAME".jar"|grep -v "grep"|awk '{print $2}'|head -n 1`
if [ -z "$PID" ]; then
echo "ERROR: The $SERVICE_NAME does not started!"
exit 1
fi
echo "Find server name [$SERVICE_NAME] macth PID is: $PID"
echo -e "Stopping the $SERVICE_NAME ..."
#kill target pid
kill -9 $PID
echo "Stopped $SERVICE_NAME [ $PID ] Done!"
3:idea中添加配置