BASH

Run Multiple Dev Servers Concurrently

Discover a simple bash script to simultaneously start multiple web development servers (e.g., frontend, backend, mock API) in the background, streamlining your local development workflow.

#!/bin/bash

echo "Starting frontend server..."
(cd frontend && npm start) &
FRONTEND_PID=$!

echo "Starting backend API server..."
(cd backend && npm run dev) &
BACKEND_PID=$!

echo "Starting mock API server..."
(cd mock-api && yarn start) &
MOCKAPI_PID=$!

cleanup() {
  echo -e "
Shutting down servers..."
  kill $FRONTEND_PID $BACKEND_PID $MOCKAPI_PID 2>/dev/null
  echo "Servers stopped."
}

trap cleanup EXIT

echo "All servers started. Press Ctrl+C to stop them."
wait
How it works: This script allows you to start multiple development servers (e.g., a frontend app, a backend API, and a mock server) in parallel using `&` to run processes in the background. It captures their process IDs (PIDs) and uses a `trap cleanup EXIT` to gracefully shut down all started processes when the script exits (e.g., by pressing Ctrl+C). This simplifies the setup for complex local development environments.

Need help integrating this into your project?

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

Hire DigitalCodeLabs