创建并设置脚本权限:

[root@distribute01 ~]# touch /etc/init.d/memached && chmod +x /etc/init.d/memcached

编辑/etc/init.d/memcached,并添加如下内容:

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

PORT=11211
USER=nobody
MAXCONN=1024
CACHESIZE=64
LISTEN="$(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1)"
OPTIONS="-l $LISTEN"
MEMCACHED_BIN="/usr/local/memcached-1.5.1/bin/memcached"

if [ -f /etc/sysconfig/memcached ];then 
        . /etc/sysconfig/memcached
fi

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
        exit 0
fi

RETVAL=0
prog="memcached"

start () {
        echo -n $"Starting $prog: "
        # insure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
        chown $USER /var/run/memcached
    fi

        daemon $MEMCACHED_BIN -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
        echo -n $"Stopping $prog: "
        killproc $MEMCACHED_BIN -QUIT
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
            rm -f /var/lock/subsys/memcached
            rm -f /var/run/memcached.pid
        fi
}

restart () {
        stop
        start
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status memcached
        ;;
  restart|reload|force-reload)
        restart
        ;;
  condrestart)
        [ -f /var/lock/subsys/memcached ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
        exit 1
esac

exit $?