Last active
December 4, 2015 23:25
-
-
Save Knetic/0204a06df5e545250d34 to your computer and use it in GitHub Desktop.
Starts and stops local redis instances by index, so that you don't need to remember commands/pids/files to do so
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# lored.sh | |
# Starts and stops local redis instances by index | |
# so that you don't need to manually type "redis-server --port...." and then later manually `kill` them. | |
# Author; George Lester | |
# Date; 2015/12 | |
### | |
# The port number immediately before the first redis instance. | |
# A range starting at "5999" will cause the redis instance "1" to use port "6000" | |
PORT_RANGE_START=5999 | |
# Modify this parameter if you'd like to add other arguments to each redis' flags. | |
ADDITIONAL_ARGUMENTS="" | |
function usage() | |
{ | |
echo "Usage: " | |
echo "" | |
echo "lored <command> <index> (config file)" | |
echo "" | |
echo "command: The actual action to take ('start'/'stop'/'stopAll')" | |
echo "index: Which server instance to perform the command upon" | |
echo "config (optional): A path to a file to be used as redis config" | |
} | |
function determineIndex() | |
{ | |
# Make sure that the second argument is numeric | |
integerRegex='^[0-9]+$' | |
if ! [[ "$1" =~ $integerRegex ]]; | |
then | |
echo "Index '$1' must be an integer" | |
exit 1 | |
fi | |
# And that it's > 0 | |
if [[ "$1" < 1 ]]; | |
then | |
echo "Index must be > 0" | |
exit 1 | |
fi | |
portIndex=$(($PORT_RANGE_START + $1)) | |
} | |
function start() | |
{ | |
local configPath="" | |
echo "Starting redis on port $portIndex" | |
args="--daemonize yes --port "$portIndex" $ADDITIONAL_ARGUMENTS" | |
if ! [ -z "$1" ]; | |
then | |
args="$1 $args" | |
fi | |
redis-server $args | |
} | |
function stop() | |
{ | |
instance=$(ps aux | grep redis-server | grep "$portIndex" | grep -v grep | tr -s ' ' | cut -d' ' -f2) | |
if [ -z "$instance" ]; | |
then | |
echo "Redis was not running, doing nothing." | |
return | |
fi | |
if [ -z "$portIndex" ]; | |
then | |
echo "Stopping all redis instances" | |
else | |
echo "Stopping redis on port $portIndex" | |
fi | |
echo "$instance" | xargs kill | |
} | |
function stopAll() | |
{ | |
stop "" | |
} | |
# Are we stopping all instances? | |
if [ "$1" == 'stopAll' ]; | |
then | |
stopAll | |
exit 0 | |
fi | |
# Otherwise we must be operating on one instance | |
if [ $# != 2 ] && [ $# != 3 ]; | |
then | |
usage | |
exit 1 | |
fi | |
determineIndex $2 | |
if [ $1 == "start" ]; | |
then | |
start $3 | |
exit 0 | |
fi | |
if [ $1 == "stop" ]; | |
then | |
stop | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment