1
0
forked from noxious/server

Added start bash file

This commit is contained in:
Dennis Postma 2025-02-09 02:27:40 +01:00
parent 8e652f8dcb
commit 7b4674587a

100
start.sh Normal file
View File

@ -0,0 +1,100 @@
# Function to cleanup on exit
cleanup() {
log "Shutting down..."
stop_server
exit 0
}
# Trap Ctrl+C (SIGINT) and other termination signals
trap cleanup SIGINT SIGTERM
#!/bin/bash
# Log function for better visibility
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
# Function to check if node server is running
is_server_running() {
if pgrep -f "npm run start" > /dev/null; then
return 0 # Server is running
else
return 1 # Server is not running
fi
}
# Function to stop the server
stop_server() {
if is_server_running; then
log "Stopping Node server..."
pkill -f "npm run start"
sleep 2 # Wait for the server to stop
if is_server_running; then
log "ERROR: Failed to stop Node server"
exit 1
else
log "Node server stopped successfully"
fi
else
log "Node server is not running"
fi
}
# Function to start the server
start_server() {
if ! is_server_running; then
log "Starting Node server..."
npm run start &
sleep 2 # Wait for the server to start
if is_server_running; then
log "Node server started successfully"
else
log "ERROR: Failed to start Node server"
exit 1
fi
else
log "Node server is already running"
fi
}
# Function to restart the server
restart_server() {
log "Restarting Node server..."
stop_server
start_server
}
# Pull updates from main branch
log "Pulling updates from main branch..."
if ! git pull origin main; then
log "ERROR: Failed to pull updates from Git"
exit 1
fi
log "Git pull completed successfully"
# Stop the server
stop_server
# Run database migrations
log "Running database migrations..."
if ! npx mikro-orm-esm migration:up; then
log "ERROR: Failed to run database migrations"
exit 1
fi
log "Database migrations completed successfully"
# Start the server
start_server
# Monitor server and restart if it stops
log "Starting server monitoring..."
while true; do
if ! is_server_running; then
log "Server stopped unexpectedly. Restarting..."
start_server
fi
sleep 10 # Check every 10 seconds
done