Skip to content

Instantly share code, notes, and snippets.

@alirezapla
Created September 30, 2025 06:27
Show Gist options
  • Select an option

  • Save alirezapla/031f0b99f1f54127dd070ab49827b54d to your computer and use it in GitHub Desktop.

Select an option

Save alirezapla/031f0b99f1f54127dd070ab49827b54d to your computer and use it in GitHub Desktop.
#!/bin/bash
# set -x
set -e
declare -a PIDS
LOG_DIR="logs"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DEBUG_PORT=5000
mkdir -p "$LOG_DIR"
cleanup() {
echo "Caught signal, killing background processes..."
for pid in "${PIDS[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
kill "$pid"
fi
done
rm $LOG_DIR/*_$TIMESTAMP.log
exit 0
}
trap cleanup SIGINT SIGTERM
start_service() {
local jar_path="$1"
local service_name="$2"
local port="$3"
if [ -f "$jar_path" ]; then
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:"$port" -Xms256m -Xmx512m -jar "$jar_path" > "$LOG_DIR/${service_name}.log" 2>&1 &
local pid=$!
PIDS+=($pid)
echo "Started $service_name service (PID: $pid) - logs: $LOG_DIR/${service_name}.log"
else
echo "Error: JAR file not found: $jar_path"
exit 1
fi
}
if [ "$1" = "--no-build" ] || [ "$1" = "-n" ]; then
echo "Skipping build process..."
else
echo "Building all modules..."
if [ -f "pom.xml" ]; then
echo "Building parent project..."
mvn clean package -DskipTests -q
if [ $? -ne 0 ]; then
echo "Error building project"
exit 1
fi
else
modules=("a" "b")
for module in "${modules[@]}"; do
if [ -d "$module" ]; then
echo "Building $module..."
cd "$module" || exit 1
mvn clean package -DskipTests -q
if [ $? -ne 0 ]; then
echo "Error building $module"
exit 1
fi
cd - > /dev/null || exit 1
else
echo "Warning: Module directory $module not found"
fi
done
fi
echo "All modules built successfully!"
fi
echo "Starting services..."
services=(
"a/target/a-0.10.0-SNAPSHOT.jar:a"
"b/target/b-0.10.0-SNAPSHOT.jar:b"
)
async_services=(
"Y/target/y-0.9.0-SNAPSHOT.jar:y"
)
sync_services=(
"X/target/X-0.7.0-SNAPSHOT.jar:x"
)
if [ "$2" = "--sync" ] || [ "$2" = "-s" ]; then
echo "Starting sync services..."
for service in "${sync_services[@]}"; do
((DEBUG_PORT += 5))
jar_path="${service%%:*}"
service_name="${service##*:}"
start_service "$jar_path" "$service_name" "$DEBUG_PORT"
done
elif [ "$2" = "--async" ] || [ "$2" = "-a" ]; then
echo "Starting async services..."
for service in "${async_services[@]}"; do
((DEBUG_PORT += 5))
jar_path="${service%%:*}"
service_name="${service##*:}"
start_service "$jar_path" "$service_name" "$DEBUG_PORT"
done
else
echo "invalid tag..."
fi
for service in "${services[@]}"; do
((DEBUG_PORT += 5))
jar_path="${service%%:*}"
service_name="${service##*:}"
start_service "$jar_path" "$service_name" "$DEBUG_PORT"
done
echo "Started all services. Press Ctrl+C to stop all."
echo "Background PIDs: ${PIDS[*]}"
echo "Log files: $LOG_DIR/*_$TIMESTAMP.log"
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment