Skip to content

Instantly share code, notes, and snippets.

@duggan
Last active December 15, 2015 06:09
Show Gist options
  • Save duggan/5214562 to your computer and use it in GitHub Desktop.
Save duggan/5214562 to your computer and use it in GitHub Desktop.
Process management of redis-server on OSX.
#!/bin/sh
##
# Process management for Redis on OSX
#
# To install:
#
# git clone https://gist.github.com/5214562.git && \
# mv 5214562/redis.sh /usr/local/bin/redis && \
# chmod +x /usr/local/bin/redis
#
# After:
# If you haven't modified the homebrew installation yet,
# run `redis daemonize`, and you'll be able to use a daemonized
# redis now!
#
# COMMANDS
#
# redis start starts redis-server
# redis stop stops redis-server
# redis status returns running status
# redis restart stops and starts the process
# redis daemonize Modifies config to run as a daemon
##
set -o nounset
set -o errexit
# Locations set by Homebrew formula
PIDFILE=/usr/local/var/run/redis.pid
CONFIGFILE=/usr/local/etc/redis.conf
# Wrap printf statements so we don't sprinkle newlines everywhere
message () {
printf "$*\n"
}
getpid () {
if [ -f $PIDFILE ] ; then
cat $PIDFILE
else
exit 1
fi
}
running () {
if [ "x$(getpid)x" = "xx" ] ; then
exit 1
fi
ps -p "$(getpid)" > /dev/null || exit 1
}
redis_start () {
if $(running) ; then
message "$(redis_status)"
else
message "starting..."
redis-server /usr/local/etc/redis.conf || exit 1
message "$(redis_status)"
fi
}
redis_stop () {
if $(running) ; then
message "stopping..."
kill -3 "$(getpid)"
else
message "not running."
fi
}
redis_status () {
if $(running); then
message "running:" "$(getpid)"
else
message "not running."
fi
}
daemonize_config () {
# The idea of executing this terrifies me, but it should do the trick.
local tempfile=$(mktemp -t tempredis.XXXX)
message "rewriting config..."
cp $CONFIGFILE "$CONFIGFILE.bck"
sed "s/daemonize no/daemonize yes/" $CONFIGFILE > $tempfile && \
mv $tempfile $CONFIGFILE
message "done."
}
case $* in
"start" )
redis_start
;;
"stop" )
redis_stop
;;
"status" )
redis_status
;;
"restart" )
redis_stop
sleep 1
redis_start
;;
"daemonize" )
read -p "Do you wish to rewrite the redis config? (y/n) " yn
case $yn in
[Yy]* )
daemonize_config
;;
[Nn]* )
exit 0
;;
esac
message "The original config was backed up to $CONFIGFILE.bck"
;;
* )
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment