ebe2ab0cdfee0b762b4ed9f0f2f0ab30a60c30d2
[awesomized/libmemcached] / memcached / scripts / memcached-init
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
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 FILES=(/etc/memcached_*.conf)
39 # check for alternative config schema
40 if [ -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;
67 else
68 CONFIGS=(memcached)
69 fi;
70
71 CONFIG_NUM=${#CONFIGS[@]}
72 for ((i=0; i < $CONFIG_NUM; i++)); do
73 NAME=${CONFIGS[${i}]}
74 PIDFILE="/var/run/${NAME}.pid"
75
76 case "$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 ;;
108 esac
109 done;
110
111 exit 0