跳转至

Memcached 服务启动脚本


2015-06-09 by dongnan

功能

启动、关闭、查看状态 memcached 服务。

环境

操作系统: CentOS 6.5

使用

使用之前请先编辑脚本文件,设置 memcached 参数:

拷贝文件

cp memcached /etc/init.d/

赋予可执行权限

chmod +x /etc/init.d/memcached

设置服务开机启动

chkconfig --add memcached
chkconfig memcached on

脚本文件

#!/bin/bash
#
# chkconfig: - 85 15
# script_name:memcached
# description:memcached daemon
# 20150609 by dongnan
# config: none
# pidfile: /var/run/memcached.pid
#
### BEGIN INIT INFO
# Provides: memcache
### END INIT INFO

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

#variables
memcache_pid='/var/run/memcached.pid'
memcached="/usr/local/memcached/bin/memcached"
memory="8192"
connect="20000"
listen_ip="10.0.100.10"
port="11211"

#function
function _start() {
    if [ ! -e "$memcache_pid" ];then
        $memcached -u root -d -m $memory -c $connect -l $listen_ip -p $port -P $memcache_pid
        echo "memcache service start.......OK"
        return 0
    else
        echo "memcache service is running !"
        return 1
    fi
}

function _stop() {

    if [ -e "$memcache_pid" ];then
         kill $(cat $memcache_pid)
         echo "memcache service stop.......OK"
         sleep 1
         test -e "$memcache_pid" && rm -rf "$memcache_pid"
         return 0
    else
         echo "memcache service is not running !"
         return 1

    fi
}


function _status() {

    if [ -e "$memcache_pid" ];then
         echo "memcache service is running !"
         return 0
    else
         echo "memcache service is not running !"
         return 1
    fi

}

#main
case "$1" in

        start)
               _start
                ;;
        stop)
               _stop
                ;;
        status)
               _status
               ;;
        *)
          echo  "Usage: $0 {start|stop|status}"
esac



回到页面顶部