Chinaunix首页 | 论坛 | 博客
  • 博客访问: 114247
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 344
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-21 23:11
文章分类
文章存档

2014年(23)

2013年(3)

分类: 网络与安全

2014-05-08 20:27:38

工作需求,需要自己写一个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 $"\033[35mStarting $prog:\033[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 "\033[32mRun $prog Successful!\033[0m"
       else
          echo -e "\033[41;33mRun $prog Fail!\033[0m"
       fi;
       exit 1
    else
       echo -e "\033[41;37m$prog has been running!Don't repeat\033[0m"
       exit 0
    fi;
  }
stop () {
    if [ -n "$PID" ]; then
        echo -n -e $"\033[35mStopping $prog:\033[0m"
        #sudo kill $PID
        start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON
        if [ `echo $?` -eq 0 ] ; then
                echo -e "\033[32mStop $prog Successfull!\033[0m"
        else
                echo -e "\033[41;33Stop $prog Fail!Please try again!\033[0m"
        fi;
    else
        echo -e "\033[34m$prog Not Running!\033[0m"
    fi;
}
restart () {
    echo -n -e $"\033[35mStopping $prog:\033[0m"
        start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON
        if [ `echo $?` -eq 0 ] ; then
            echo -e "\033[32mStop $prog Successfull!\033[0m"
        else
            echo -e "\033[41;33Stop $prog Fail!Please try again!\033[0m"
        fi;
    echo -n -e $"\033[35mStarting $prog:\033[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 "\033[32mRun $prog Successful!\033[0m"
        else
            echo -e "\033[41;33mRun $prog Fail!\033[0m"
        fi;
        exit 1
}
status () {
  if [ ! -n "$PID" ]; then
    echo -e "\033[32m$prog Stopped!\033[0m"
  else
    echo -e "\033[32m$prog Running!\033[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 $?
阅读(3987) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

CU博客助理2014-07-11 15:52:40

专家点评:Linux总是那么通透、那么可信、那么可靠,我们总可以根据自己的要求进行DIY。博主的文章很实用,谢谢博主的分享,分享就是一种美。感谢参加原创评选活动,获奖结果即将公布)