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
89    restart|force-reload)
90    #
91    #   If the "reload" option is implemented, move the "force-reload"
92    #   option to the "reload" entry above. If not, "force-reload" is
93    #   just the same as "restart".
94    #
95        echo -n "Restarting $DESC: "
96        start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
97        rm -f $PIDFILE
98        sleep 1
99        start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
100        echo "$NAME."
101        ;;
102    *)
103        N=/etc/init.d/$NAME
104    # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
105        echo "Usage: $N {start|stop|restart|force-reload}" >&2
106        exit 1
107        ;;
108esac
109done;
110
111exit 0
112