Skip to content

Instantly share code, notes, and snippets.

@u1i
Created November 6, 2025 04:59
Show Gist options
  • Select an option

  • Save u1i/1dc6ad4f76089897955ef135557ef5bc to your computer and use it in GitHub Desktop.

Select an option

Save u1i/1dc6ad4f76089897955ef135557ef5bc to your computer and use it in GitHub Desktop.
Run Redis on a specific port and dedicated config file
#!/bin/bash
# Check if a port number was provided
if [ -z "$1" ]; then
echo "Usage: $0 <port_number>"
echo "Example: $0 6380"
exit 1
fi
PORT=$1
# Define file paths based on the port number
CONFIG_FILE="/etc/redis/redis_$PORT.conf"
DATA_DIR="/var/lib/redis_$PORT"
LOG_FILE="/var/log/redis/redis_$PORT.log"
PID_FILE="/var/run/redis/redis_$PORT.pid"
DEFAULT_CONFIG="/etc/redis/redis.conf"
# Check if the default config exists
if [ ! -f "$DEFAULT_CONFIG" ]; then
echo "❌ Error: Default Redis configuration file not found at $DEFAULT_CONFIG."
echo "Please ensure Redis is installed or update the DEFAULT_CONFIG path."
exit 1
fi
echo "--- Setting up Redis instance on port $PORT ---"
# 1. Create the dedicated data and log directories
if [ ! -d "$DATA_DIR" ]; then
sudo mkdir -p "$DATA_DIR"
echo "Created data directory: $DATA_DIR"
fi
sudo mkdir -p /var/log/redis
# 2. Create the new configuration file ONLY IF IT DOES NOT EXIST (Configuration Guard)
if [ -f "$CONFIG_FILE" ]; then
echo "Configuration file **ALREADY EXISTS**: $CONFIG_FILE"
echo "Skipping configuration creation to prevent override."
else
echo "Creating new configuration file: $CONFIG_FILE"
# Copy the default configuration file
sudo cp "$DEFAULT_CONFIG" "$CONFIG_FILE"
# Use | (pipe) as the delimiter in sed commands
# Update Port
sudo sed -i "s|port 6379|port $PORT|" "$CONFIG_FILE"
# Update PID file path
sudo sed -i "s|pidfile /var/run/redis/redis-server.pid|pidfile $PID_FILE|" "$CONFIG_FILE"
# Update Log file path
sudo sed -i "s|^logfile \"\"|logfile $LOG_FILE|" "$CONFIG_FILE"
# Update Data Directory path
sudo sed -i "s|^dir .*$|dir $DATA_DIR|" "$CONFIG_FILE"
# Ensure a unique RDB file name
sudo sed -i "s|dbfilename dump.rdb|dbfilename dump_$PORT.rdb|" "$CONFIG_FILE"
# ⭐ PERSISTENCE FIX: UNCOMMENT/ENABLE default RDB save points
# This uses a pattern (^[[:space:]]*#) to match the start of a line containing optional spaces and a #
sudo sed -i "/^[[:space:]]*# save 900 1/s/^#//" "$CONFIG_FILE"
sudo sed -i "/^[[:space:]]*# save 300 10/s/^#//" "$CONFIG_FILE"
sudo sed -i "/^[[:space:]]*# save 60 10000/s/^#//" "$CONFIG_FILE"
# Final cleanup to remove any leading whitespace left after removing '#'
sudo sed -i 's/^[[:space:]]*//' "$CONFIG_FILE"
echo "Configuration written successfully."
fi
# 3. Check if a server is already running on this port before attempting to start
if sudo ss -tuln | grep ":$PORT " > /dev/null; then
echo "⚠️ A process is already listening on port $PORT. Check PID file: $PID_FILE"
echo " Stopping the script."
exit 1
fi
# 4. Start the Redis server
echo "Starting Redis server on port $PORT using config $CONFIG_FILE..."
sudo redis-server "$CONFIG_FILE"
if [ $? -eq 0 ]; then
echo "✅ Redis server on port $PORT started successfully."
echo "Connect with: redis-cli -p $PORT"
else
echo "❌ Failed to start Redis server on port $PORT."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment