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