首页 > 互联网技术, 数据库 > 我的tc启动脚本

我的tc启动脚本

2009年12月8日 2,406 views 发表评论 阅读评论

tc (Tokyo Cabinet)的启动脚本.
供参考.具体的调优,参考官网.

#! /bin/sh
#----------------------------------------------------------------
# Startup script for the server of Tokyo Tyrant
# added by gary 20091130
#----------------------------------------------------------------
# 大概2亿bnum占用400M内存
# configuration variables 
prog="ttservctl"
cmd="ttserver"
#host_ip=`/sbin/ifconfig eth1 | grep 'inet' | awk '{print $2}' | sed -e "s/addr\://"` 
port="11211"
basedir="$HOME/garychen_data_${port}"
pidfile="$basedir/${port}.pid"
logfile="$basedir/${port}.log"
ulogdir="/home/garychen/garychen_ulog_${port}"
ulimsiz="512m"
sid=1111
thnum=16
mhost="111.111.111.111"
mport="11211"
rtsfile="$basedir/${port}.rts"
dbname="$basedir/garychen${port}.tch#bnum=100000000#xmsiz=3221225472"
maxcon="65535"
retval=0


# setting environment variables
LANG=C
LC_ALL=C
PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"
export LANG LC_ALL PATH

# start the server
start(){
  printf 'Starting the server of Tokyo Tyrant\n'
  mkdir -p "$basedir"
  if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] || [ -z "$dbname" ] ; then
    printf 'Invalid configuration\n'
    retval=1
  elif ! [ -d "$basedir" ] ; then
    printf 'No such directory: %s\n' "$basedir"
    retval=1
  elif [ -f "$pidfile" ] ; then
    pid=`cat "$pidfile"`
    printf 'Existing process: %d\n' "$pid"
    retval=1
  else
    if [ -n "$maxcon" ] ; then
      ulimit -n "$maxcon" >/dev/null 2>&1
    fi
    cmd="$cmd -port $port -dmn -pid $pidfile"
    if [ -n "$logfile" ] ; then
      cmd="$cmd -log $logfile "
    fi
    if [ -n "$ulogdir" ] ; then
      mkdir -p "$ulogdir"
      cmd="$cmd -ulog $ulogdir"
    fi
    if [ -n "$ulimsiz" ] ; then
      cmd="$cmd -ulim $ulimsiz"
    fi
    if [ -n "$sid" ] ; then
      cmd="$cmd -sid $sid"
    fi
    if [ -n "$thnum" ] ; then
      cmd="$cmd -thnum $thnum"
    fi
    if [ -n "$mhost" ] ; then
      cmd="$cmd -mhost $mhost"
    fi
    if [ -n "$mport" ] ; then
      cmd="$cmd -mport $mport"
    fi
    if [ -n "$rtsfile" ] ; then
      cmd="$cmd -rts $rtsfile"
    fi
    printf "Executing: %s\n" "$cmd"
    cmd="$cmd $dbname"
    $cmd
    if [ "$?" -eq 0 ] ; then
      printf 'Done\n'
    else
      printf 'The server could not started\n'
      retval=1
    fi
  fi
}


# stop the server
stop(){
  printf 'Stopping the server of Tokyo Tyrant\n'
  if [ -f "$pidfile" ] ; then
    pid=`cat "$pidfile"`
    printf "Sending the terminal signal to the process: %s\n" "$pid"
    kill -TERM "$pid"
    c=0
    while true ; do
      sleep 0.1
      if [ -f "$pidfile" ] ; then
        c=`expr $c + 1`
        if [ "$c" -ge 100 ] ; then
          printf 'Hanging process: %d\n' "$pid"
          retval=1
          break
        fi
      else
        printf 'Done\n'
        break
      fi
    done
  else
    printf 'No process found\n'
    retval=1
  fi
}


# send HUP to the server for log rotation
hup(){
  printf 'Sending HUP signal to the server of Tokyo Tyrant\n'
  if [ -f "$pidfile" ] ; then
    pid=`cat "$pidfile"`
    printf "Sending the hangup signal to the process: %s\n" "$pid"
    kill -HUP "$pid"
    printf 'Done\n'
  else
    printf 'No process found\n'
    retval=1
  fi
}

# sync the server 
sync(){
  printf "Synchronize updated contents with the database file of Tokyo Cabinet $port \n"
  tcrmgr sync -port $port 127.0.0.1
  if [ $? = "0" ] ; then
    printf 'Done\n'
  else
    printf 'sync failed.\n'
    retval=1
  fi
}

# check permission
if [ -d "$basedir" ] && ! touch "$basedir/$$" >/dev/null 2>&1
then
  printf 'Permission denied\n'
  exit 1
fi
rm -f "$basedir/$$"


# dispatch the command
case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  stop
  start
  ;;
hup)
  hup
  ;;
sync)
 sync
  ;;
*)
  printf 'Usage: %s {start|stop|restart|hup|sync}\n' "$prog"
  exit 1
  ;;
esac


# exit
exit "$retval"

# END OF FILE
 » 转载保留版权:老陈 » 《我的tc启动脚本》
 » 如果喜欢可以: 点此订阅本站
分类: 互联网技术, 数据库 标签: , ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.