| 1 |
#! /bin/bash |
| 2 |
### BEGIN INIT INFO |
| 3 |
# Provides: memcached |
| •4 |
# Required-Start: $remote_fs $syslog |
| 5 |
# Required-Stop: $remote_fs $syslog |
| 6 |
# Should-Start: $local_fs |
| 7 |
# Should-Stop: $local_fs |
| 8 |
# Default-Start: 2 3 4 5 |
| 9 |
# Default-Stop: 0 1 6 |
| 10 |
# Short-Description: memcached - Memory caching daemon |
| 11 |
# Description: memcached - Memory caching daemon |
| 12 |
### END INIT INFO |
| 13 |
|
| 14 |
# Usage: |
| 15 |
# cp /etc/memcached.conf /etc/memcached_server1.conf |
| 16 |
# cp /etc/memcached.conf /etc/memcached_server2.conf |
| 17 |
# start all instances: |
| 18 |
# /etc/init.d/memcached start |
| 19 |
# start one instance: |
| 20 |
# /etc/init.d/memcached start server1 |
| 21 |
# stop all instances: |
| 22 |
# /etc/init.d/memcached stop |
| 23 |
# stop one instance: |
| 24 |
# /etc/init.d/memcached stop server1 |
| 25 |
# There is no "status" command. |
| 26 |
|
| 27 |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| 28 |
DAEMON=/usr/bin/memcached |
| 29 |
DAEMONNAME=memcached |
| 30 |
DAEMONBOOTSTRAP=/usr/share/memcached/scripts/start-memcached |
| 31 |
DESC=memcached |
| 32 |
|
| 33 |
test -x $DAEMON || exit 0 |
| 34 |
test -x $DAEMONBOOTSTRAP || exit 0 |
| 35 |
|
| 36 |
set -e |
| 37 |
|
| •38 |
if [ -f /etc/default/memcached ] ; then |
| 39 |
. /etc/default/memcached |
| 40 |
fi |
| 41 |
|
| 42 |
. /lib/lsb/init-functions |
| 43 |
|
| 44 |
FILES=(/etc/memcached_*.conf) |
| 45 |
# check for alternative config schema |
| 46 |
if [ -r "${FILES[0]}" ]; then |
| 47 |
CONFIGS=() |
| 48 |
for FILE in "${FILES[@]}"; |
| 49 |
do |
| 50 |
# remove prefix |
| 51 |
NAME=${FILE#/etc/} |
| 52 |
# remove suffix |
| 53 |
NAME=${NAME%.conf} |
| 54 |
|
| 55 |
# check optional second param |
| 56 |
if [ $# -ne 2 ]; |
| 57 |
then |
| 58 |
# add to config array |
| 59 |
CONFIGS+=($NAME) |
| 60 |
elif [ "memcached_$2" == "$NAME" ]; |
| 61 |
then |
| 62 |
# use only one memcached |
| 63 |
CONFIGS=($NAME) |
| 64 |
break; |
| 65 |
fi; |
| 66 |
done; |
| 67 |
|
| 68 |
if [ ${#CONFIGS[@]} == 0 ]; |
| 69 |
then |
| 70 |
echo "Config not exist for: $2" >&2 |
| 71 |
exit 1 |
| 72 |
fi; |
| 73 |
else |
| 74 |
CONFIGS=(memcached) |
| 75 |
fi; |
| 76 |
|
| 77 |
CONFIG_NUM=${#CONFIGS[@]} |
| 78 |
for ((i=0; i < $CONFIG_NUM; i++)); do |
| 79 |
NAME=${CONFIGS[${i}]} |
| 80 |
PIDFILE="/var/run/${NAME}.pid" |
| 81 |
|
| 82 |
case "$1" in |
| 83 |
start) |
| 84 |
echo -n "Starting $DESC: " |
| 85 |
start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE |
| 86 |
echo "$NAME." |
| 87 |
;; |
| 88 |
stop) |
| 89 |
echo -n "Stopping $DESC: " |
| •90 |
start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON |
| 91 |
echo "$NAME." |
| 92 |
rm -f $PIDFILE |
| 93 |
;; |
| 94 |
status) |
| 95 |
[ $# -lt 2 ] && NAME=$DAEMONNAME |
| 96 |
PIDFILE="/var/run/$NAME.pid" |
| 97 |
set +e |
| 98 |
start-stop-daemon --status --pidfile $PIDFILE |
| 99 |
case $? in |
| 100 |
0) echo "$DESC: $NAME (pid $(cat $PIDFILE)) is running" && exit 0;; |
| 101 |
1) echo "$DESC: $NAME is not running thro' the pid file exists" && rm -f $PIDFILE && exit 1;; |
| 102 |
3) echo "$DESC: $NAME is not running" && exit 3;; |
| 103 |
4) echo "$DESC: $NAME status is unclear, sorry" && exit 4;; |
| 104 |
esac |
| 105 |
;; |
| 106 |
restart|force-reload) |
| 107 |
# |
| 108 |
# If the "reload" option is implemented, move the "force-reload" |
| 109 |
# option to the "reload" entry above. If not, "force-reload" is |
| 110 |
# just the same as "restart". |
| 111 |
# |
| 112 |
echo -n "Restarting $DESC: " |
| •113 |
start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE |
| 114 |
rm -f $PIDFILE |
| 115 |
sleep 1 |
| 116 |
start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE |
| 117 |
echo "$NAME." |
| 118 |
;; |
| 119 |
*) |
| 120 |
N=/etc/init.d/$NAME |
| 121 |
# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 |
| 122 |
echo "Usage: $N {start|stop|status|restart|force-reload}" >&2 |
| 123 |
exit 1 |
| 124 |
;; |
| 125 |
esac |
| 126 |
done; |
| 127 |
|
| 128 |
exit 0 |