BASH
Start and Monitor a Local Web Development Server
Automate the startup of local development servers (e.g., Node.js, Python, PHP) and optionally tail their logs for real-time monitoring, streamlining your development workflow.
#!/bin/bash
SERVER_COMMAND="npm start" # Customize: e.g., "python -m http.server 8000" or "php -S localhost:8000"
LOG_FILE="server.log"
echo "Starting local development server..."
# Start the server in the background and redirect output to log file
# Use 'nohup' to keep it running after terminal close, or just '&' for current session
nohup $SERVER_COMMAND > "$LOG_FILE" 2>&1 &
SERVER_PID=$! # Get the PID of the last background command
echo "Server started with PID: $SERVER_PID. Output redirected to $LOG_FILE"
# Optional: Tail the log file for real-time output
echo "Tailing server logs (Ctrl+C to stop tailing, server continues running):"
tail -f "$LOG_FILE"
# After tailing stops, give instructions to kill the server
echo "
Server is still running in the background (PID: $SERVER_PID)."
echo "To stop the server, run: kill $SERVER_PID"
# A more robust solution might involve trap commands for cleanup or process management tools.
How it works: This script starts a local development server process (e.g., a Node.js `npm start` command) in the background, redirecting its output to a log file. It then tails the log file, allowing the developer to see real-time output. This is highly useful for automating dev server startup, debugging, and maintaining an overview of server activity without tying up the terminal, and provides instructions on how to terminate the process later.