← Back to all snippets
BASH

Manage a Local Node.js Development Server

A Bash script to easily start and stop a Node.js development server (e.g., Express, React app) using its process ID, streamlining local development workflows.

#!/bin/bash

# Usage:
#   ./dev_server.sh start <command> (e.g., ./dev_server.sh start "npm run dev")
#   ./dev_server.sh stop
#   ./dev_server.sh status

PID_FILE=".server.pid"

start_server() {
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p "$PID" > /dev/null; then
            echo "Server is already running with PID $PID."
            exit 0
        else
            echo "Stale PID file found. Removing it."
            rm "$PID_FILE"
        fi
    fi

    if [ -z "$1" ]; then
        echo "Error: Start command is required. E.g., '$0 start \"npm run dev\"'"
        exit 1
    fi

    COMMAND="$@"
    echo "Starting server with command: $COMMAND"
    nohup bash -c "$COMMAND" > server.log 2>&1 & # Run in background, log output
    echo $! > "$PID_FILE"
    echo "Server started with PID $(cat "$PID_FILE"). Log available in server.log"
}

stop_server() {
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p "$PID" > /dev/null; then
            echo "Stopping server with PID $PID..."
            kill "$PID"
            rm "$PID_FILE"
            echo "Server stopped."
        else
            echo "No running server found with PID $PID. Removing stale PID file."
            rm "$PID_FILE"
        fi
    else
        echo "No PID file found. Server might not be running or wasn't started with this script."
    fi
}

server_status() {
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p "$PID" > /dev/null; then
            echo "Server is running with PID $PID."
            echo "Check server.log for output."
        else
            echo "Server PID $PID found in .pid file, but process is not running. Stale PID file?"
            echo "Consider running '$0 stop' to clean up."
        fi
    else
        echo "Server is not running (no PID file found)."
    fi
}

case "$1" in
    start)
        shift
        start_server "$@"
        ;;
    stop)
        stop_server
        ;;
    status)
        server_status
        ;;
    *)
        echo "Usage: $0 {start \"<command>\"|stop|status}"
        exit 1
        ;;
esac
How it works: This Bash script simplifies the process of starting and stopping a local Node.js development server, a common task for web developers working with frameworks like Express, Next.js, or React. It uses a PID file to keep track of the running process. The `start` command backgrounds the server process and logs its output, `stop` gracefully terminates it, and `status` checks if the server is currently running. This helps in managing multiple development services and cleaning up processes efficiently without manually searching for PIDs.

Need help integrating this into your project?

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

Hire DigitalCodeLabs