Browse files

Wrapper script to automatically restart memcached if it dies. By defa…

…ult it

expects the underlying memcached binary to be called "memcached.real" (use
the -M option to change that). This script is intended to be named
/usr/local/bin/memcached so anything that tries to start memcached will
start this script instead.

Reviewed by Dustin.
Tested by running memcached with various options and killing it; the script
	restarted it with the same options each time.
To revert, just run the underlying memcached command directly.
  • Loading branch information...
1 parent 6b7a770 commit 3a2c7235041d44a0a21d281cfe958b0466ea6b3b sgrimm committed Nov 21, 2006
Showing with 48 additions and 0 deletions.
  1. +48 −0 scripts/memcached-wrapper
View
48 scripts/memcached-wrapper
@@ -0,0 +1,48 @@
+#!/bin/sh
+#
+# Scan the list of arguments, pulling out any that we want to intercept.
+#
+WANT_BG=
+ARGS=""
+MEMCACHED="memcached.real"
+while [ $# -gt 0 ]; do
+ case "$1" in
+ -M)
+ shift
+ MEMCACHED="$1"
+ shift
+ ;;
+ -d)
+ WANT_BG=1
+ # Don't run memcached with -d
+ shift
+ ;;
+ *)
+ # Accumulate other args. This will probably not work
+ # so well if the args contain spaces, but we don't
+ # use spaces in our args.
+ ARGS="$ARGS $1"
+ shift
+ ;;
+ esac
+done
+
+#
+# Put ourselves in the background if we were run with the -d option.
+#
+if [ "$WANT_BG" = 1 ]; then
+ if [ -z "$WRAPPER_BACKGROUNDED" ]; then
+ WRAPPER_BACKGROUNDED=1
+ export WRAPPER_BACKGROUNDED
+ (nohup sh $0 -M $MEMCACHED $ARGS < /dev/null 2>&1 > /dev/null &)
+ exit 0
+ fi
+fi
+
+#
+# Now run memcached, restarting it if it fails.
+#
+while true; do
+ syslog -s -l info "Starting memcached"
+ $MEMCACHED $ARGS
+done

0 comments on commit 3a2c723

Please sign in to comment.