123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #!/bin/bash
- SERVER="all"
- WEB_CHAT_START_PAGE_JARS="./server"
- KILL_TIMEOUT=5
- if [ $# -gt 0 ]; then
- SERVER="${1%.*}"
- fi
- echo "Starting server: $SERVER"
- echo "=== Webchat packaging begins ==="
- mvn clean package -DskipTests=true
- echo "=== Webchat packaging completed ==="
- rm -rf "$WEB_CHAT_START_PAGE_JARS"
- mkdir -p "$WEB_CHAT_START_PAGE_JARS"
- echo "Old jars removed"
- find . -path "*/target/*.jar" -exec cp {} "$WEB_CHAT_START_PAGE_JARS" \;
- cd "$WEB_CHAT_START_PAGE_JARS" || exit 1
- kill_old_process() {
- local jar_name=$1
- local pid_list=$(pgrep -f "java.*$jar_name")
- if [ -n "$pid_list" ]; then
- echo "Found existing process(es) for $jar_name: $pid_list"
-
- kill $pid_list
- echo "Sent SIGTERM to process(es), waiting for termination..."
-
- local timeout=0
- while kill -0 $pid_list 2>/dev/null && [ $timeout -lt $KILL_TIMEOUT ]; do
- sleep 1
- ((timeout++))
- done
-
- if kill -0 $pid_list 2>/dev/null; then
- echo "Force killing process(es)..."
- kill -9 $pid_list
- fi
-
- if pgrep -f "java.*$jar_name" >/dev/null; then
- echo "Error: Failed to kill process for $jar_name!" >&2
- return 1
- fi
- fi
- return 0
- }
- if [ "$SERVER" == "all" ]; then
- echo "Starting ALL services..."
- for jar in *.jar; do
- service_name="${jar%.*}"
-
- if ! kill_old_process "$jar"; then
- exit 1
- fi
-
- echo "Starting $service_name..."
- nohup java -jar "$jar" > "${service_name}.log" 2>&1 &
- done
- echo "All services restarted in background."
- else
- target_jar="${SERVER}.jar"
- if [ -f "$target_jar" ]; then
-
- if ! kill_old_process "$target_jar"; then
- exit 1
- fi
-
- echo "Starting $target_jar..."
- nohup java -jar "$target_jar" > "${SERVER}.log" 2>&1 &
- echo "Service restarted. Tracking logs:"
- tail -100f "${SERVER}.log"
- else
- echo "Error: Jar file $target_jar not found!"
- echo "Available jars:"
- ls -1 *.jar | sed 's/.jar$//'
- exit 1
- fi
- fi
- check_port_conflict() {
- local port=$1
- if lsof -i :$port &>/dev/null; then
- echo "Port $port is still in use after process restart!" >&2
- return 1
- fi
- return 0
- }
|