BASH

Managing Concurrent Background Services for Local Development

Efficiently start and manage multiple development services (e.g., frontend, backend, database) in parallel within a single terminal session using Bash.

#!/bin/bash

# Function to gracefully stop all background processes
cleanup() {
    echo "Stopping all background services..."
    kill $(jobs -p) 2>/dev/null
    echo "All services stopped."
}

# Trap SIGINT (Ctrl+C) to run cleanup function
trap cleanup SIGINT

echo "Starting Backend Service..."
# Replace with your actual backend start command
npm run start:backend &
BACKEND_PID=$!
echo "Backend Service (PID: $BACKEND_PID) started."

echo "Starting Frontend Service..."
# Replace with your actual frontend start command
npm run start:frontend &
FRONTEND_PID=$!
echo "Frontend Service (PID: $FRONTEND_PID) started."

echo "All services running. Press Ctrl+C to stop them."

# Wait for all background jobs to complete (or for Ctrl+C)
wait
How it works: This script demonstrates how to run multiple services concurrently in the background using `&` and manage them with `wait`. The `trap` command ensures that all spawned background processes are gracefully terminated when the script receives a `SIGINT` signal (e.g., from Ctrl+C), preventing orphaned processes.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs