memcached-1.4.13 + repcached 自动安装脚本

#!/bin/bash
# Author:Jian
# Date:2017-11-16
# Version:0.1
# Description: 自动安装memcached-1.4.13并自动打上对应版本的repcached-1.4.13补丁,适用于CentOS64位系统,在CentOS6/7上通过测试
url1="http://memcached.org/files/old/memcached-1.4.13.tar.gz"
url2="http://mdounin.ru/files/repcached-2.3.1-1.4.13.patch.gz"
url3="https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz"
string=`cat /etc/redhat-release`
vers=`echo ${string##*release}|sed 's/^[[:space:]]*//g'|awk -F"." '{print $1}'`
download_dir=/usr/local/src/

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin:/usr/local/memcached/bin

if which memcached &>/dev/null ; then
   echo -e "\033[31mYou have installed memcached on your system,please uninstall it first!\033[0m"
   exit 1
fi

check_user() {
if [ $UID -ne 0 ] ;then
   echo -e "\033[31mYou must be root to run this script!\033[0m" 1>&2
   exit 1
fi
}
install_progs() {
for prog in `echo $@`
do
if ! which $prog &>/dev/null; then
   echo "Installing $prog ..." 
   yum install $prog -y &>/dev/null 
   if [ $? -eq 0 ];then
      echo "Install $prog OK" 
   else
      echo "Download $prog failed,exit" 
      exit 2
   fi
fi
done
}

download() {
   cd $download_dir
   for url in `echo $@`
   do 
      if [ ! -e ${download_dir}${url##*/} ];then
         echo "Downloading ${url##*/} ..."
         if wget -q -t 8 -c -T 3 $url ;then 
            echo "Download ${url##*/} OK." 
         else
            echo -e "\033[31mDownload ${url##*/} failed.\033[0m"
         fi
      else 
         echo "File ${download_dir}${url##*/} exists."
      fi
   done
}


# libevent-2.1.8-stable.tar.gz
install_libevent() {
   cd $download_dir
   if [ -f /usr/local/libevent/lib/libevent-2.1.so.6 ];then
      echo "Libevent has installed on your system"
   else 
      echo "Installing libevent..."
      tar xf ${url3##*/} &>/dev/null
      cd $(echo "${url3##*/}" |sed 's/\.tar\.gz//g')
      ./configure --prefix=/usr/local/libevent
      make && make install
      echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
      ldconfig
   fi

}

install_memcached() {
cd $download_dir
tar xf ${url1##*/} &>/dev/null
gzip -d -c ${download_dir}${url2##*/} > ${download_dir}repcached-2.3.1-1.4.13.patch
cd $(echo "${url1##*/}" |sed 's/\.tar\.gz//g')
patch -p1 <../repcached-2.3.1-1.4.13.patch
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent --enable-replication --enable-64bit
make && make install
echo 'export PATH=/usr/local/memcached/bin:$PATH' > /etc/profile.d/memcached.sh
source /etc/profile.d/memcached.sh
}

check_user
install_progs wget gcc
download $url1 $url2 $url3
if [ -f ${download_dir}${url1##*/} ] && [ -f ${download_dir}${url2##*/} ] && [ -f ${download_dir}${url3##*/} ];then
   install_libevent
   install_memcached
else 
   echo "There is a lack of necessary installation packages,please check."
   echo "You can download the package manually and put the package into: /usr/local/src"
   exit 1
fi
if [ -f /usr/local/memcached/bin/memcached ];then
   echo -e "\033[32mCongratulations,you have successfuly installed memcached!\033[0m" 
   cd $download_dir
   rm -rf ${download_dir}$(echo "${url1##*/}" |sed 's/\.tar\.gz//g')
   rm -rf ${download_dir}$(echo "${url3##*/}" |sed 's/\.tar\.gz//g')
else
   echo -e "\033[31mInstalled memcached failed,please check!\033[0m"
fi 
echo 'PORT="11211"' > /etc/sysconfig/memcached
echo 'MAXCONN="1024"' >> /etc/sysconfig/memcached
echo 'CACHESIZE="64"' >> /etc/sysconfig/memcached
echo 'OPTIONS=""' >> /etc/sysconfig/memcached

if id -u memcached &>/dev/null || id -g memcached &>/dev/null;then
   echo -e "\033[31mUser or group 'memcached' exists and now use 'nobody'as a user\033[0m"
   echo 'USER="nobody"' >> /etc/sysconfig/memcached
else
   groupadd -r memcached
   useradd -r -s /sbin/nologin -g memcached memcached
   echo 'USER="memcached"' >> /etc/sysconfig/memcached
   chown -R memcached:memcached /usr/local/memcached
fi
exit 0

#############################################################################
#   下面分为CentOS6、CentOS7提供的服务脚本                                  #
#############################################################################
###centos6服务脚本,将其写入/etc/rc.d/init.d/memcached文件###

#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached

. /etc/rc.d/init.d/functions
. /etc/sysconfig/memcached

## Default variables

RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile="/var/lock/subsys/memcached"

start() {
        echo -n $"Starting $desc (memcached): "
        daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success && touch $lockfile || failure
        echo
        return $RETVAL
}

stop() {
        echo -n $"Shutting down $desc (memcached): "
        killproc $prog
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure
        echo
        return $RETVAL
}

restart() {
        stop
        start
}

reload() {
        echo -n $"Reloading $desc ($prog): "
        killproc $prog -HUP
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        return $RETVAL
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  condrestart)
        [ -e $lockfile ] && restart
        RETVAL=$?
        ;;       
  reload)
        reload
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL

###centos7服务脚本,将其写入/usr/lib/systemd/system/memcached.service文件###

[Unit]
Description=Memcached
Before=httpd.service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/local/memcached/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

[Install]
WantedBy=multi-user.target


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值