ubuntu下memcached启动与停止脚本

发布时间:2019-12-26编辑:脚本学堂
分享一个ubuntu系统中memcached启动与停止脚本,有研究memcached的朋友可以参考下,用脚本管理linux服务方便好用,希望对大家有所帮助。

一个memcached的启动停止脚本。  
 

复制代码 代码示例:
#! /bin/sh
# ckconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/memcached.conf
# Source function library - for suse linux
. /lib/lsb/init-functions
PORT=11111
USER=memcache
#最大连接数,根据实际需求修改
MAXCONN=1024
#最大内存量,单位M
CACHESIZE=400
PID=`ps -ef|grep memcache|grep -v grep|awk '{print $2}'`
prog="Memcached"
PIDFILE="/tmp/memcache.pid"
DAEMON=/usr/bin/memcached
start () {
    if [ ! -n "$PID" ]; then
       echo -n -e $"33[35mStarting $prog:33[0m"
       /usr/bin/memcached -m $CACHESIZE -l 127.0.0.1 -p $PORT -d -u $USER -c $MAXCONN -P /tmp/memcache.pid
       if [ `echo $?` == 0 ];then
          echo -e "33[32mRun $prog Successful!33[0m"
       else
          echo -e "33[41;33mRun $prog Fail!33[0m"
       fi;
       exit 1
    else
       echo -e "33[41;37m$prog has been running!Don't repeat33[0m"
       exit 0
    fi;
  }
stop () {
    if [ -n "$PID" ]; then
        echo -n -e $"33[35mStopping $prog:33[0m"
        #sudo kill $PID
        start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON
        if [ `echo $?` -eq 0 ] ; then
                echo -e "33[32mStop $prog Successfull!33[0m"
        else
                echo -e "33[41;33Stop $prog Fail!Please try again!33[0m"
        fi;
    else
        echo -e "33[34m$prog Not Running!33[0m"
    fi;
}
restart () {
    echo -n -e $"33[35mStopping $prog:33[0m"
        start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON
        if [ `echo $?` -eq 0 ] ; then
            echo -e "33[32mStop $prog Successfull!33[0m"
        else
            echo -e "33[41;33Stop $prog Fail!Please try again!33[0m"
        fi;
    echo -n -e $"33[35mStarting $prog:33[0m"
        /usr/bin/memcached -m $CACHESIZE -l 127.0.0.1 -p $PORT -d -u $USER -c $MAXCONN -P /tmp/memcache.pid
        if [ `echo $?` == 0 ];then
            echo -e "33[32mRun $prog Successful!33[0m"
        else
            echo -e "33[41;33mRun $prog Fail!33[0m"
        fi;
        exit 1
}
status () {
  if [ ! -n "$PID" ]; then
    echo -e "33[32m$prog Stopped!33[0m"
  else
    echo -e "33[32m$prog Running!33[0m"
  fi;
}
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac
exit $?

脚本小编总结:
这个memcached启动与停止脚本写的还是不错的,其中用到了bash语言中的不少知识点,包括自定义函数与case语句的用法,脚本中参数传递的方法,大家好好研究下。

>>> 猜你喜欢:linux下memcache memcached安装与配置教程大全