memcached是优秀的开源高性能分布式内存对象缓存系统。在linux系统下编译安装后没有自动注册为系统服务。另外由于memcached基于libevent库,该库默认的编译安装位置为/usr/local/lib目录,该目录又不是多数linux发行版的默认库加载路径。
因此,在执行memcached之前需要修改默认加载路径,将该路径包含进去。虽然只需要设置一次但毕竟要设置,很麻烦。
如果你又希望存放在该路径下的库不被程序自动搜寻到,就不能使用该方法。那么怎么才能有个完全的方法来解决呢?
解决方法:
既然memcached没有被注册为系统服务,那我们手动地去注册。注册系统服务需要编写启动脚本,一般要实现三个方法,分别是:start、stop和restart。并且该脚本要放置在/etc/init.d/目录中。
服务脚本(脚本需要以root权限编写):
 
复制代码 代码示例:
#chkconfig: 345 60 60   
#description: this service is from memcached    
# which is high performance object cache system   
#!/bin/sh   
export ld_library_path=/usr/local/lib:$ld_library_path  
memcached_process_name="memcached"  
start(){  
  echo "starting memcached service with port 11211"  
  # note that the character ` is not single quotation,    
  # it is the left character of "1" key in your keyboard   
  memcached_pid_list=`pidof $memcached_process_name`  
  if test -n "$memcached_pid_list"  
  then  
    echo "fail to launch memcached, since it has already started"  
    exit 1  
  else  
    echo "launching memcached with maxmemory 64mb"  
    /usr/local/bin/memcached -l 0.0.0.0 -p 11211 -m 64 -d -u root  
    echo "launch memcached successfully"  
    exit 0  
  fi;  
}  
  
stop(){  
  echo "stopping memcached service..."  
  memcached_pid_list=`pidof $memcached_process_name`  
  if test -n "$memcached_pid_list"  
  then  
    echo "find memcached process(es), start to end them"  
    kill -9 $memcached_pid_list  
    if test "$?" = "0"  
    then  
      echo "success to terminate all memcached processes"  
    else  
      echo "can not terminate all memcached processes"  
    fi;  
    echo {1}quot;finished stopping memcached service"  
    exit 0  
  else  
    echo "can not find any memcached process, fail to stop service"  
    exit 1  
  fi;  
}  
  
  
case "$1" in  
  start)  
     start  
     ;;  
  stop)  
     stop  
     ;;  
  restart)  
     stop  
     #sleep 3 seconds to wait for process's exit   
     sleep 3  
     start  
     ;;  
  *)  
     echo {1}quot;usage:$0 {start|stop|restart}"  
     exit 2  
esac  
 
编写完毕后。对其赋予755权限,即rwxr-xr-x(文件所有者具有读写执行权限,同组用户具有只读和执行权限,其他用户具有只读和执行权限):
 
复制代码 代码示例:
[root@lxp2 init.d]# chmod 755 memcached  
 
脚本中指定了memcached监听本地所有ip,端口为tcp的11211端口,默认为其分配64m的内存,如果想修改这些值,需要修改上面的配置文件start函数的如下两行:
 
echo "launching memcached with maxmemory 64mb"  
/usr/local/bin/memcached -l 0.0.0.0 -p 11211 -m 64 -d -u root 
具体参数意义请参阅memcache的手册man memcached。
手动启动、停止和重启服务命令为
 
复制代码 代码示例:
service memcached start
service memcached stop
service memcached restart
虽然编写了脚本,但是现在还不能让系统自动加载服务,接下来:
在上述脚本中注意开始的三行:
 
复制代码 代码示例:
#chkconfig: 345 60 60   
#description:this service is from memcached    
# which is high performance object cache system  
#chkconfig和#description是必须要有的。
chkconfig后的第一段数字:345,表示在那些运行界别中会开启此服务。
当linux系统以指定的运行级别来运行时,进入系统会自动调用服务的start;
当llinux系统以非指定的运行级别来运行时,进入系统后自动调用服务的stop来阻止服务启动。
运行级别有7个等级:
 
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无
网络连接的多用户
命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
以上介绍了在linux中通过shell/ target=_blank class=infotextkey>shell脚本将memcached注册为服务的方法,并介绍了chkconfig命令的用法,以及linux系统运行等级的相关知识,希望对大家有所帮助。
>>> 猜你喜欢:linux下memcache memcached安装与配置教程大全