Java_Manage_script/manage_descend_v2.sh
2025-06-23 14:25:37 +02:00

183 lines
4.3 KiB
Bash

#!/bin/bash
MAIN_CLASS=descend.admin.Server
jarFile=Descend-1.10.3.jar
applicationsDeploymentPath=/opt/applications
applicationName=S6_Descend
thisApplicationPath=$applicationsDeploymentPath/$applicationName
libDir=$thisApplicationPath/lib
distDir=$thisApplicationPath/dist
runDir=$thisApplicationPath/run
logFile=$thisApplicationPath/logs/${applicationName}-GENERAL.log
pidFile=$runDir/${applicationName}.pid
jarToLoad=$distDir/$jarFile
JAVA_CMD=/opt/software/jdk1.8.0_241/bin/java
appPort=1235
MAX_MEMORY=2560m
MIN_MEMORY=256m
MIN_PERM=256m
MAX_PERM=512m
keystore=$thisApplicationPath/certs/IMGHTTP_2026.pfx
keypass=$(cat $thisApplicationPath/certs/p12.pass)
NodeIP=85.112.179.22
cd "$thisApplicationPath" || {
echo "Failed to change directory to $thisApplicationPath"
exit 1
}
function check_status {
if [ ! -f "$pidFile" ]; then
return 1
fi
pid=$(tail -1 "$pidFile" 2>/dev/null)
if [ -z "$pid" ]; then
return 1
fi
if ps -p "$pid" > /dev/null 2>&1; then
if netstat -an | grep ":$appPort" | grep LISTEN > /dev/null; then
return 0
else
# Process running but port not listening - partial failure
return 1
fi
else
# Process not running
return 1
fi
}
function check_pid {
if [ ! -f "$pidFile" ]; then
return 1
fi
pid=$(tail -1 "$pidFile" 2>/dev/null)
if [ -z "$pid" ]; then
return 1
fi
if ps -p "$pid" > /dev/null 2>&1; then
return 0
else
return 1
fi
}
function stop_process {
echo "Checking if process is running..."
check_pid
if [ $? -eq 0 ]; then
pid=$(tail -1 "$pidFile")
echo "Process running with pid $pid, stopping..."
kill "$pid"
# Wait for process to die gracefully
for i in {1..10}; do
sleep 1
if ! ps -p "$pid" > /dev/null 2>&1; then
echo "Process stopped gracefully."
break
fi
done
# If app doesn't die after normal kill, force kill
if ps -p "$pid" > /dev/null 2>&1; then
echo "Process did not stop, sending SIGKILL..."
kill -9 "$pid"
sleep 1
fi
# Clean up PID file
> "$pidFile"
echo "Process killed and PID file cleared."
else
echo "Process not running."
fi
}
function start_process {
if [ "$2" != "" ]; then
MAIN_CLASS="$2"
echo "Using custom main class: $MAIN_CLASS"
else
echo "Using default main class: $MAIN_CLASS"
fi
echo "Building CLASSPATH..."
CLASSPATH="$jarToLoad:"
for file in "$libDir"/*.jar; do
CLASSPATH="${CLASSPATH}${file}:"
done
echo "Starting Descend process..."
ulimit -n 65535
nohup $JAVA_CMD \
-Djavax.net.ssl.keyStoreType=pkcs12 \
-Djavax.net.ssl.keyStore="$keystore" \
-Djavax.net.ssl.keyStorePassword="$keypass" \
-cp "$CLASSPATH" \
-Xmx$MAX_MEMORY -Xms$MIN_MEMORY \
-XX:PermSize=$MIN_PERM -XX:MaxPermSize=$MAX_PERM \
-DAPPNAME=$applicationName \
-Djava.rmi.server.hostname=$NodeIP \
-Dcom.sun.management.jmxremote=true \
-Dcom.sun.management.jmxremote.port=$appPort \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=true \
"$MAIN_CLASS" > "$logFile" 2>&1 &
echo $! > "$pidFile"
sleep 5
check_pid
if [ $? -eq 0 ]; then
echo "Process started with pid $(tail -1 $pidFile)"
nohup curl -u "admincontrol:tuow90MNW" \
http://servercs6.msgtoolbox.com/smsadm/synced/scendadmin/descendsmpp/activateall.php \
> /dev/null 2>&1 &
exit 0
else
echo "Process failed to stay up."
stop_process
exit 1
fi
}
#Main
case "$1" in
start)
start_process "$@"
;;
stop)
stop_process
;;
status)
check_status
if [ $? -eq 0 ]; then
echo "Process running with pid $(tail -1 "$pidFile")"
exit 0
else
echo "Process not running"
exit 3
fi
;;
restart)
stop_process
start_process "$@"
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac