首页 > 互联网技术 > memcached的启动脚本

memcached的启动脚本

2010年3月28日 2,476 views 发表评论 阅读评论

一个memcached的启动脚本.

#! /bin/sh
#----------------------------------------------------------------
# Startup script for the server of memcached
# 
#----------------------------------------------------------------
#export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
basedir=$HOME/app/memcached/bin
cmd=${basedir}/memcached
port="11211"
pidfile="$basedir/${port}.pid"
max_memory=1024
# default: 64M
max_simul_conn=2048
# default: 1024
maxcon=51200
#growth_factor=1.3
# default: 1.25
thread_num=6
# default: 4
# 注释以下,可查看详细启动信息
#verbose="-vv"
# 以何种协议启动
#bind_protocol=binary  
# ascii, binary, or auto (default)
retval=0

# start the server
start(){
  printf 'Starting the server of memcached\n'
  if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] ; then
    printf 'Invalid configuration\n'
    retval=1
  elif ! [ -d "$basedir" ] ; then
    printf 'No such directory: %s\n' "$basedir"
    retval=1
  else
      # 已经验证了基本参数,启动实例,如果已经存在实例,那么不启动.
      psmemcache1=`ps x | grep memcached | grep $port |grep -v "grep"| wc -l`
      if [ "$psmemcache1" != "0" ]; then
          printf 'memcached server found.\n'
          retval=1
      else
            if [ -n "$maxcon" ] ; then
              ulimit -n "$maxcon" >/dev/null 2>&1
            fi
            cmd="$cmd -p $port -d -P $pidfile"
            if [ -n "$max_memory" ] ; then
              cmd="$cmd -m $max_memory"
            fi
            if [ -n "$max_simul_conn" ] ; then
              cmd="$cmd -c $max_simul_conn"
            fi
            if [ -n "$growth_factor" ] ; then
              cmd="$cmd -f $growth_factor"
            fi
            if [ -n "$verbose" ] ; then
              cmd="$cmd $verbose"
            fi
            printf "Executing: %s\n" "$cmd"
            $cmd
            if [ "$?" -eq 0 ] ; then
              printf 'Done\n'
            else
              printf 'The server could not started\n'
              retval=1
            fi
       fi
   fi
}


# stop the server
stop(){
  printf 'Stopping the server of memcached\n'
  # memcached的pidfile类似haproxy,进程死亡后,pidfile仍然存在,下次启动,会重新初始化pidfile;
  # 所以可能存在pidfile是错误的,如启动memcached,如果已经存在实例,那么pidfile写入了pid,但是实例却并不能真正起来..
  # 所以用另一种方式尝试kill memcached.
  psmemcache1=`ps x | grep memcached | grep $port |grep -v "grep"| wc -l`
  if [ "$psmemcache1" = "0" ]; then
     printf 'no memcached server found.\n'
     retval=1
  else
     ps x |grep memcached |grep $port |grep -v "grep" |awk '{ print $1 }' | xargs kill
     sleep 0.5
     psmemcache1=`ps x | grep memcached | grep $port |grep -v "grep"| wc -l`
     if [ "$psmemcache1" = "0" ]; then
         printf 'Done\n'
     else
         printf 'can not stop memcached server \n'
         retval=1
     fi
   fi
}


# dispatch the command
case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  stop
  start
  ;;
*)
  printf 'Usage: %s {start|stop|restart}\n' "$prog"
  exit 1
  ;;
esac
# exit
exit "$retval"
# END OF FILE
 » 转载保留版权:老陈 » 《memcached的启动脚本》
 » 如果喜欢可以: 点此订阅本站
分类: 互联网技术 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.