BASH
Start Multiple Background Services for Development
Create a convenient Bash script to concurrently start and manage multiple development services, like a local server and a watcher, in the background.
#!/bin/bash
echo "Starting development services..."
# Start a simple Python HTTP server (for static files)
python3 -m http.server 8000 &
SERVER_PID=$!
echo "Python HTTP server started on port 8000 (PID: ${SERVER_PID})"
# Start a file watcher (e.g., using `watchexec` or a simple loop)
# Example with a dummy watcher that just sleeps
# In a real scenario, this would be `npm run watch` or `webpack --watch`
( while true; do echo "Watching files..."; sleep 5; done ) &
WATCHER_PID=$!
echo "File watcher started (PID: ${WATCHER_PID})"
echo "All services started in the background. Press [Enter] to stop them."
read -r
echo "Stopping services..."
kill ${SERVER_PID}
kill ${WATCHER_PID}
echo "All services stopped."
How it works: This script simplifies starting multiple services required for a web development environment. It launches a Python HTTP server and a dummy file watcher (which can be replaced by `npm run watch` or similar build tools) in the background. It stores their process IDs (PIDs) and waits for user input to gracefully terminate all started services, making it easy to manage a local development setup with a single command.