← Back to all snippets
BASH

Managing Local Development Services

Create a convenient Bash script to start, stop, or restart multiple local development services like databases or cache servers with a single command.

#!/bin/bash

SERVICE_MYSQL="/usr/local/bin/mysql.server" # Adjust path as needed
SERVICE_REDIS="redis-server"

function start_services() {
  echo "Starting MySQL..."
  sudo "$SERVICE_MYSQL" start
  echo "Starting Redis..."
  "$SERVICE_REDIS" --daemonize yes
  echo "All services started."
}

function stop_services() {
  echo "Stopping MySQL..."
  sudo "$SERVICE_MYSQL" stop
  echo "Stopping Redis..."
  pkill redis-server # Find and kill the redis process
  echo "All services stopped."
}

function restart_services() {
  stop_services
  sleep 2
  start_services
  echo "All services restarted."
}

case "$1" in
  start)
    start_services
    ;;
  stop)
    stop_services
    ;;
  restart)
    restart_services
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
How it works: This script provides a simple command-line interface to manage multiple local development services. Users can pass `start`, `stop`, or `restart` as arguments to control services like MySQL and Redis, streamlining their development workflow by consolidating commands. Ensure service paths are correct for your environment.

Need help integrating this into your project?

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

Hire DigitalCodeLabs