方法一:
在/etc/rc.d/rc.local 加入以下代码
/usr/local/memcached/bin/memcached -u root -d -m 2048 -l 192.168.70.1 -p 11211 -P /tmp/memcached.pid// 所有机器都能访问/usr/local/memcached/bin/memcached -u root -d -m 2048 -l 0.0.0.0 -p 11211 -P /tmp/memcached.pid// 不指定ip,默认只有本机访问/usr/local/memcached/bin/memcached -u deamon -d -m 2048 -p 11211 -P /tmp/memcached.pid
用户最好是 apache或 deamon
权限 (权限不够,无法开启启动memcached)
# cd /etc/rc.d# chmod 777 rc.local// 或者# chmod 777 /etc/rc.d/rc.local
方法二:
// 写服务代码 vim /etc/init.d/memcached
粘贴以下代码
注意2处:memcached的安装路径
#! /bin/sh## chkconfig: - 55 45# description: The memcached daemon is a network memory cache service.# processname: memcached# config: /etc/sysconfig/memcached # Source function library.. /etc/rc.d/init.d/functions PORT=11211USER=rootMAXCONN=1024CACHESIZE=64OPTIONS="" if [ -f /etc/sysconfig/memcached ];then . /etc/sysconfig/memcachedfi # Check that networking is up.if [ "$NETWORKING" = "no" ]then exit 0fi RETVAL=0 start () { echo "Starting memcached ..." # insure that /var/run/memcached has proper permissions chown $USER /usr/local/memcached/bin/memcached // 注意1:memcached安装路径 // 注意2:memcached安装路径 /usr/local/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached}stop () { echo "Stopping memcached ..." killproc memcached 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) restart ;; condrestart) [ -f /var/lock/subsys/memcached ] && restart || : ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}" exit 1esac exit $?
#cd /etc/init.d#chmod 775 memcached//将memcached加入系统服务#chkconfig --add memcached#chkconfig memcached on//以服务方式运行memcached
启动与关闭
#service memcached start启动/etc/rc.d/init.d/memcached start /etc/rc.d/init.d/memcached stop/etc/rc.d/init.d/memcached restart