File size: 2,667 Bytes
4aec76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/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