#!/usr/bin/env bash # Helper to start/stop/status the redis-commander UI used for local development. # Usage: ./scripts/redis-commander.sh start|stop|status set -euo pipefail PIDFILE="/tmp/redis-commander.pid" LOGFILE="/tmp/redis-commander.log" PORT=8081 function _which_rc() { if command -v redis-commander >/dev/null 2>&1; then command -v redis-commander return 0 fi echo "" >/dev/stderr return 1 } case ${1-} in start) # If a process is already listening on the port, assume redis-commander is running if command -v lsof >/dev/null 2>&1; then EXIST_PID=$(lsof -iTCP:$PORT -sTCP:LISTEN -t || true) if [ -n "$EXIST_PID" ]; then echo "redis-commander already running (pid=$EXIST_PID) listening on port $PORT" echo "$EXIST_PID" > "$PIDFILE" exit 0 fi fi if [ -f "$PIDFILE" ] && kill -0 "$(cat $PIDFILE)" >/dev/null 2>&1; then echo "redis-commander already running (pid=$(cat $PIDFILE))" exit 0 fi RC_BIN=$(command -v redis-commander || true) if [ -z "$RC_BIN" ]; then echo "redis-commander not found in PATH. Install with: npm install -g redis-commander" >&2 exit 2 fi echo "Starting redis-commander on port $PORT (logs: $LOGFILE)"; nohup "$RC_BIN" --port "$PORT" > "$LOGFILE" 2>&1 & echo $! > "$PIDFILE" echo "Started (pid=$(cat $PIDFILE))" ;; stop) if [ ! -f "$PIDFILE" ]; then echo "No pidfile ($PIDFILE) found. Is redis-commander running?"; exit 0 fi PID=$(cat "$PIDFILE") if kill -0 "$PID" >/dev/null 2>&1; then echo "Stopping redis-commander (pid=$PID)"; kill "$PID" || true sleep 0.2 rm -f "$PIDFILE" echo "Stopped"; else echo "Process $PID not running; removing stale pidfile"; rm -f "$PIDFILE" fi ;; status) if [ -f "$PIDFILE" ]; then PID=$(cat "$PIDFILE") if kill -0 "$PID" >/dev/null 2>&1; then echo "redis-commander running (pid=$PID)"; exit 0 else echo "pidfile present but process not running (pid=$PID)"; # stale pidfile rm -f "$PIDFILE" fi fi # If no pidfile, check whether something is listening on the port if command -v lsof >/dev/null 2>&1; then EXIST_PID=$(lsof -iTCP:$PORT -sTCP:LISTEN -t || true) if [ -n "$EXIST_PID" ]; then echo "redis-commander appears to be running (pid=$EXIST_PID) listening on port $PORT"; echo "$EXIST_PID" > "$PIDFILE" exit 0 fi fi echo "redis-commander not running"; exit 1 ;; *) echo "Usage: $0 start|stop|status" >&2 exit 2 ;; esac