feiyang10086 发表于 2011-11-23 14:17

Linux中将memcached注册为系统服务 .

Linux中将memcached注册为系统服务 .








引言:

memcached是优秀的开源高性能分布式内存对象缓存系统。在Linux系统下编译安装后没有自动注册为系统服务。另外由于memcached基于libevent库,该库默认的编译安装位置为/usr/local/lib目录,该目录又不是多数linux发行版的默认库加载路径。因此在执行memcached之前需要修改默认加载路径,将该路径包含进去。虽然只需要设置一次但毕竟要设置,很麻烦。如果你又希望存放在该路径下的库不被程序自动搜寻到,就不能使用该方法。那么怎么才能有个完全的方法来解决呢?



解决方法:

既然memcached没有被注册为系统服务,那我们手动地去注册。注册系统服务需要编写启动脚本,一般要实现三个方法,分别是:start、stop和restart。并且该脚本要放置在/etc/init.d/目录中。下面是我写的服务脚本(脚本需要以root权限编写):view plaincopy to clipboardprint?
01.#chkconfig: 345 60 60   
02.#description: This Service is from memcached \   
03.# which is high performance object cache system   
04.#!/bin/sh   
05.export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
06.memcached_process_name="memcached"
07.start(){
08.echo "Starting Memcached Service with Port 11211"
09.# note that the character ` is not single quotation, \   
10.# It is the left character of "1" key in your keyboard   
11.memcached_pid_list=`pidof $memcached_process_name`
12.if test -n "$memcached_pid_list"
13.then
14.    echo "Fail To Launch Memcached, Since It Has Already Started"
15.    exit 1
16.else
17.    echo "Launching memcached with MaxMemory 64MB"
18.    /usr/local/bin/memcached -l 0.0.0.0 -p 11211 -m 64 -d -u root
19.    echo "Launch Memcached Successfully"
20.    exit 0
21.fi;
22.}
23.
24.stop(){
25.echo "Stopping Memcached Service..."
26.memcached_pid_list=`pidof $memcached_process_name`
27.if test -n "$memcached_pid_list"
28.then
29.    echo "Find Memcached Process(es), Start To End Them"
30.    kill -9 $memcached_pid_list
31.    if test "$?" = "0"
32.    then
33.      echo "Success to Terminate All Memcached Processes"
34.    else
35.      echo "Can Not Terminate All Memcached Processes"
36.    fi;
37.    echo {1}quot;Finished Stopping Memcached Service"
38.    exit 0
39.else
40.    echo "Can Not Find Any Memcached Process, Fail To Stop Service"
41.    exit 1
42.fi;
43.}
44.
45.
46.case "$1" in
47.start)
48.   start
49.   ;;
50.stop)
51.   stop
52.   ;;
53.restart)
54.   stop
55.   #sleep 3 seconds to wait for process's exit   
56.   sleep 3
57.   start
58.   ;;
59.*)
60.   echo {1}quot;Usage:$0 {start|stop|restart}"
61.   exit 2
62.esac
#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(文件所有者具有读写执行权限,同组用户具有只读和执行权限,其他用户具有只读和执行权限):view plaincopy to clipboardprint?
01.# chmod 755 memcached
# chmod 755 memcached 脚本中指定了memcached监听本地所有IP,端口为TCP的11211端口,默认为其分配64M的内存,如果想修改这些值,需要修改上面的配置文件start函数的如下两行:view plaincopy to clipboardprint?
01.echo "Launching memcached with MaxMemory 64MB"
02./usr/local/bin/memcached -l 0.0.0.0 -p 11211 -m 64 -d -u root
    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虽然编写了脚本,但是现在还不能让系统自动加载服务,接下来:

在上述脚本中注意开始的三行:view plaincopy to clipboardprint?
01.#chkconfig: 345 60 60   
02.#description:This Service is from memcached \   
03.# which is high performance object cache system
#chkconfig: 345 60 60
#description:This Service is from memcached \
# which is high performance object cache system
#chkconfig和#description是必须要有的。

chkconfig后的第一段数字:345,表示在那些运行界别中会开启此服务。当Linux系统以指定的运行级别来运行时,进入系统会关闭系统时会自动调用服务的start和stop。

运行级别有7个等级:
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动

由于memcached在工作时需要使用网络连接,所以最低级别要在3才能正常工作。

chkconfig后的第二段数字:60,表示该服务在系统启动后的脚本运行顺序。这样来说可能都看不懂。举个例子,假如一个服务被设置为系统运行等级3的时候启动,则服务管理工具会在/etc/rc3.d目录中创建该服务相关脚本的映射,映射的名称即为S[数字][服务名]。上面的脚本安装好服务后会在/etc/rc3.d、/etc/rc4.d、/etc/rc5.d中分别创建名称为S60memcached的映射,映射目标文件为/etc/init.d/memcached(即本文件)。S表示Startup,即“启动”。系统在启动后就已知在以何种级别运行,因此系统会试图执行对应级别的目录内所有脚本,并加以start参数。至于执行的顺序,就靠S后边的编号了。编号数值越大越后被执行(00~99)。

chkconfig后的第三段数字:60,和第二段数字的功能相似,但是该数字表示系统关闭后的脚本运行顺序。还是刚才的例子,假如一个服务被设置为系统运行等级3的时候启动,系统关闭的之前理所当然也要关闭它。则服务管理工具会在/etc/rc3.d目录中创建该服务相关脚本的映射,映射的名称即为K[数字][服务名]。上面的脚本安装好服务后会在/etc/rc3.d、/etc/rc4.d、/etc/rc5.d中分别创建名称为K60memcached的映射,映射目标文件为/etc/init.d/memcached(即本文件)。K表示Kill,即“杀死”。系统在关闭之前就已知在以何种级别运行的,因此系统会试图执行对应级别的目录内所有脚本,并加以stop参数。至于执行的顺序,就靠K后边的编号了。编号数值越小越后被执行(99~00)。

要想让服务随系统启动,需要安装服务:view plaincopy to clipboardprint?
01.# chkconfig --add memcached
# chkconfig --add memcached 此时查看系统的服务中就会有自己注册的服务了:view plaincopy to clipboardprint?
01.# chkconfig --list
02....
03.mdmpd         0:off   1:off   2:off   3:off   4:off   5:off   6:off
04.memcached       0:off   1:off   2:off   3:on    4:on    5:on    6:off
05.messagebus      0:off   1:off   2:off   3:on    4:on    5:on    6:off
06....
页: [1]
查看完整版本: Linux中将memcached注册为系统服务 .