Ubuntu 12.04.4 LTS 设置memcached开机自启动

本机之前已经安装过memcached服务器,并且已用于开发环境,但是每次启动机器后都要手工启动下memcached,很烦,于是就将memcached设置成开机自启动。

步骤1:sudo gedit /etc/init.d/memcached

Ubuntu 12.04.4 LTS 设置memcached开机自启动
#!/bin/sh 
# 
# Startup script for memcached 
# 
# processname: memcached 
# pidfile: /tmp/memcached.pid 
# logfile: /var/log/memcached/memcached_log
# memcached_home: /usr/local/bin/memcached
# chkconfig: 35 21 79 
# description: Start or stop memcached Service 
 
. /lib/lsb/init-functions
 
RETVAL=0 
  
prog="memcached" 
basedir=/usr/local/bin/memcached
cmd=/usr/local/bin/memcached
pidfile=/tmp/memcached.pid 
 
ip="127.0.0.1"            # 绑定侦听的IP地址 
port="11211"                    # 服务端口 
username="root"                 # 运行程序的用户身份 
max_memory=64                   # default: 64M | 最大使用内存 
max_connection=1024             # default: 1024 | 最大同时连接数 
  
start() { 
    echo -n $"Starting service: $prog" 
    $cmd -d -l$ip -p$port -u$username -m$max_memory -c$max_connection -P$pidfile 
    RETVAL=$?

    if [ ! -d /var/lock/subsys ]; then
        mkdir -p /var/lock/subsys
    fi

    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog 
} 
  
stop() { 
    echo -n $"Stopping service: $prog  " 
    current_user=$(whoami)
    pidlist=$(ps -ef | grep $current_user | grep memcached | grep -v grep | awk ‘{print $2}‘) 
    for pid in $pidlist ; do 
        kill -9 $pid 
        if [ $? -ne 0 ]; then 
           return 1 
        fi 
    done 
    RETVAL=$? 
    echo 
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog 
} 
  
case "$1" in 
    start) 
        start 
        ;; 
    stop) 
        stop 
        ;; 
    restart) 
        stop 
        start 
        ;; 
    status) 
        status memcached 
        ;; 
    *) 
        echo "Usage: $0 {start|stop|restart|status}" 
        exit 1 
esac 
  
exit $RETVAL
Ubuntu 12.04.4 LTS 设置memcached开机自启动

 

步骤2:设置启动级别

sudo chkconfig -add memcached

sudo chkconfig --level 35 memcached on

执行后,/etc/rcX.d(X等于1-5)下面就产生了SXXMemcached文件(S代表开机自启动,K代表禁止开机启动,XX为两位数字)

 

步骤3:启动服务

service memcached start

 

步骤4:检测服务是否启动

telnet 127.0.0.1 11211

 

成功后重启下机器,再执行步骤4看看结果。

 

记录/备份

Ubuntu 12.04.4 LTS 设置memcached开机自启动

上一篇:Context free grammar and Context sensitive grammar


下一篇:FineUI