应用管理脚本优化V1
This commit is contained in:
parent
95ea9f9aa3
commit
bb430e8e0c
@ -139,9 +139,9 @@ watch(visible, () => {
|
||||
</NP>
|
||||
</NUploadDragger>
|
||||
</NUpload>
|
||||
<div class="flex-center">
|
||||
<!-- <NCheckbox v-model="data.updateSupport">{{ $t('common.updateExisting') }}</NCheckbox>-->
|
||||
<!-- <n-divider vertical />-->
|
||||
<!-- <div class="flex-center">
|
||||
<!– <NCheckbox v-model="data.updateSupport">{{ $t('common.updateExisting') }}</NCheckbox>–>
|
||||
<!– <n-divider vertical />–>
|
||||
数据批次
|
||||
<NDivider vertical />
|
||||
<NDatePicker
|
||||
@ -151,7 +151,7 @@ watch(visible, () => {
|
||||
:is-date-disabled="disablePreviousDate"
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
</div>-->
|
||||
<NAlert v-if="message" :title="$t('common.importResult')" :type="success ? 'success' : 'error'" :bordered="false">
|
||||
{{ message }}
|
||||
</NAlert>
|
||||
|
||||
@ -1,11 +1,22 @@
|
||||
#!/bin/sh
|
||||
# ./cds.sh start 启动 stop 停止 restart 重启 status 状态
|
||||
AppName=mps-server.jar
|
||||
ServerName=mps-server
|
||||
|
||||
# 应用端口配置/需要与maps-server启动端口保持一致
|
||||
APP_PORT=10800
|
||||
|
||||
# JVM参数
|
||||
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms2048m -Xmx4096m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=2048m -XX:+HeapDumpOnOutOfMemoryError -XX:+UseZGC -Djasypt.encryptor.password=66668888"
|
||||
APP_HOME=`pwd`
|
||||
LOG_PATH=$APP_HOME/logs/$AppName.log
|
||||
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms2048m -Xmx4096m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=2048m -XX:+HeapDumpOnOutOfMemoryError -XX:+UseZGC -Djasypt.encryptor.password=66668888"
|
||||
APP_HOME=$(pwd)
|
||||
LOG_PATH=$APP_HOME/logs/$ServerName.log
|
||||
|
||||
# 健康检查超时时间/秒
|
||||
HEALTH_CHECK_TIMEOUT=60
|
||||
|
||||
echo_with_timestamp() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $@"
|
||||
}
|
||||
|
||||
if [ "$1" = "" ];
|
||||
then
|
||||
@ -19,44 +30,113 @@ then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
function wait_for_health_check() {
|
||||
local timeout=$1
|
||||
local pid=$2
|
||||
local elapsed=0
|
||||
local interval=2
|
||||
|
||||
while [ $elapsed -lt $timeout ]; do
|
||||
# 检查日志中的启动完成消息,暂定使用检查服务端口的方式
|
||||
#if grep -q "mps started successfully" "$LOG_PATH" 2>/dev/null; then
|
||||
# echo_with_timestamp ">>>>>>>> Check log info pass..."
|
||||
# return 0
|
||||
#fi
|
||||
|
||||
# 检查服务端口启动成功/端口启动即表示服务启动成功
|
||||
if netstat -tln | grep -q ":$APP_PORT "; then
|
||||
echo_with_timestamp ">>>>>>>> Check app server port start suc..."
|
||||
return 0
|
||||
fi
|
||||
|
||||
# app pid已经关闭,退出health eheck
|
||||
if ! ps -p $pid > /dev/null; then
|
||||
echo_with_timestamp ">>>>>>>> Check app pid existing fail..."
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo_with_timestamp ">>>>>>>> Waiting for $interval second to recheck..."
|
||||
|
||||
sleep $interval
|
||||
elapsed=$((elapsed + interval))
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function start()
|
||||
{
|
||||
PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
|
||||
echo "----------------------------------------------------------"
|
||||
|
||||
if [ x"$PID" != x"" ]; then
|
||||
echo "$AppName is running..."
|
||||
else
|
||||
nohup java $JVM_OPTS -jar $AppName > /dev/null 2>&1 &
|
||||
echo "Start $AppName success..."
|
||||
PID=$(ps -ef | grep java | grep "$AppName" | grep -v grep | awk '{print $2}')
|
||||
|
||||
if [ -n "$PID" ]; then
|
||||
echo_with_timestamp ">>>> $ServerName is already running..."
|
||||
echo "----------------------------------------------------------"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# 启动前确保日志目录存在
|
||||
mkdir -p "$APP_HOME/logs"
|
||||
|
||||
# 使用nohup启动应用并记录PID
|
||||
echo_with_timestamp ">>>> Ready to starting $ServerName on port $APP_PORT..."
|
||||
nohup java $JVM_OPTS -jar "$AppName" >> "$LOG_PATH" 2>&1 &
|
||||
local new_pid=$!
|
||||
|
||||
echo_with_timestamp ">>>> Started $ServerName with PID: $new_pid"
|
||||
|
||||
# 等待健康检查
|
||||
echo_with_timestamp ">>>>>> Starting health check..."
|
||||
if wait_for_health_check $HEALTH_CHECK_TIMEOUT $new_pid; then
|
||||
echo_with_timestamp ">>>> Start $ServerName success..."
|
||||
else
|
||||
echo_with_timestamp ">>>>>> Start $ServerName failed or health check timeout..."
|
||||
# 尝试获取退出状态 ----- 当健康检查超时而server启动成功时会卡住,不使用
|
||||
#if wait $new_pid 2>/dev/null; then
|
||||
# local exit_status=$?
|
||||
# echo_with_timestamp ">>>> Application exited with status: $exit_status"
|
||||
#fi
|
||||
echo "----------------------------------------------------------"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "----------------------------------------------------------"
|
||||
}
|
||||
|
||||
function stop()
|
||||
{
|
||||
echo "Stop $AppName"
|
||||
echo "----------------------------------------------------------"
|
||||
|
||||
echo_with_timestamp ">>>> Ready to stopping $ServerName..."
|
||||
|
||||
PID=""
|
||||
query(){
|
||||
PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
|
||||
PID=$(ps -ef | grep java | grep "$AppName" | grep -v grep | awk '{print $2}')
|
||||
}
|
||||
|
||||
query
|
||||
if [ x"$PID" != x"" ]; then
|
||||
kill -TERM $PID
|
||||
echo "$AppName (pid:$PID) exiting..."
|
||||
while [ x"$PID" != x"" ]
|
||||
if [ -n "$PID" ]; then
|
||||
echo_with_timestamp ">>>>>> Found running process with PID: $PID"
|
||||
kill -TERM "$PID"
|
||||
echo_with_timestamp ">>>>>> $ServerName (pid:$PID) exiting..."
|
||||
while [ -n "$PID" ]
|
||||
do
|
||||
sleep 1
|
||||
query
|
||||
done
|
||||
echo "$AppName exited."
|
||||
echo_with_timestamp ">>>>>> $ServerName exited."
|
||||
else
|
||||
echo "$AppName already stopped."
|
||||
echo_with_timestamp ">>>> $ServerName already stopped."
|
||||
fi
|
||||
|
||||
echo_with_timestamp ">>>> Stop $ServerName success..."
|
||||
echo "----------------------------------------------------------"
|
||||
}
|
||||
|
||||
function restart()
|
||||
{
|
||||
echo_with_timestamp ">> Ready to restarting $ServerName..."
|
||||
stop
|
||||
sleep 2
|
||||
start
|
||||
@ -64,23 +144,88 @@ function restart()
|
||||
|
||||
function status()
|
||||
{
|
||||
PID=`ps -ef |grep java|grep $AppName|grep -v grep|wc -l`
|
||||
if [ $PID != 0 ];then
|
||||
echo "$AppName is running..."
|
||||
echo "----------------------------------------------------------"
|
||||
|
||||
PID=$(ps -ef | grep java | grep "$AppName" | grep -v grep | awk '{print $2}')
|
||||
if [ -n "$PID" ]; then
|
||||
echo_with_timestamp ">> $ServerName is running (PID: $PID)"
|
||||
|
||||
local port_listening=$(netstat -tln | grep ":$APP_PORT " | wc -l)
|
||||
if [ "$port_listening" -gt 0 ]; then
|
||||
echo_with_timestamp ">> Application is listening on port $APP_PORT"
|
||||
|
||||
#需要root用户或其他用户增加netstat权限
|
||||
local port_info=$(netstat -tlnp | grep ":$APP_PORT " | grep java)
|
||||
if [ -n "$port_info" ]; then
|
||||
echo_with_timestamp ">> Port details: $port_info"
|
||||
fi
|
||||
|
||||
if command -v nc >/dev/null 2>&1; then
|
||||
if nc -z localhost $APP_PORT; then
|
||||
echo_with_timestamp ">> Port $APP_PORT is accessible"
|
||||
else
|
||||
echo_with_timestamp ">> Warning: Port $APP_PORT is listening but not accessible"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo_with_timestamp ">> Warning: Application is running but not listening on port $APP_PORT"
|
||||
fi
|
||||
|
||||
# 运行时间
|
||||
local uptime=$(ps -o etime= -p "$PID" 2>/dev/null | xargs)
|
||||
if [ -n "$uptime" ]; then
|
||||
echo_with_timestamp ">> Process uptime: $uptime"
|
||||
fi
|
||||
|
||||
# 内存使用
|
||||
local memory_usage=$(ps -o rss= -p "$PID" 2>/dev/null | xargs)
|
||||
if [ -n "$memory_usage" ]; then
|
||||
memory_usage=$((memory_usage / 1024))
|
||||
echo_with_timestamp ">> Memory usage: ${memory_usage}MB"
|
||||
fi
|
||||
|
||||
else
|
||||
echo "$AppName is not running..."
|
||||
echo_with_timestamp ">> $ServerName is not running"
|
||||
|
||||
# 端口被占用
|
||||
local port_used_by=$(netstat -tlnp | grep ":$APP_PORT " | awk '{print $7}' | cut -d'/' -f1)
|
||||
if [ -n "$port_used_by" ]; then
|
||||
echo_with_timestamp ">> Warning: Port $APP_PORT is being used by another process (PID: $port_used_by)"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "----------------------------------------------------------"
|
||||
}
|
||||
|
||||
function show_config()
|
||||
{
|
||||
echo "----------------------------------------------------------"
|
||||
echo_with_timestamp ">> Application Configuration:"
|
||||
echo_with_timestamp ">>>> Name: $ServerName"
|
||||
echo_with_timestamp ">>>> Port: $APP_PORT"
|
||||
echo_with_timestamp ">>>> Home: $APP_HOME"
|
||||
echo_with_timestamp ">>>> Log : $LOG_PATH"
|
||||
echo "----------------------------------------------------------"
|
||||
}
|
||||
|
||||
case $1 in
|
||||
start)
|
||||
start;;
|
||||
start
|
||||
;;
|
||||
stop)
|
||||
stop;;
|
||||
stop
|
||||
;;
|
||||
restart)
|
||||
restart;;
|
||||
restart
|
||||
;;
|
||||
status)
|
||||
status;;
|
||||
status
|
||||
;;
|
||||
config)
|
||||
show_config
|
||||
;;
|
||||
*)
|
||||
|
||||
echo_with_timestamp "Usage: $0 {start|stop|restart|status|config}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
Loading…
Reference in New Issue
Block a user