Memcached 安装脚本(附服务器自启动)

举报
kaliarch 发表于 2022/03/12 10:07:06 2022/03/12
【摘要】 一、目的为简化Memcached快速安装部署,并添加至服务启动项,开机自启动。 二、脚本2.1 github地址github链接2.2 脚本内容#!/bin/bash#mail:xuel@anchnet.com#function:auto install memcachedclearecho "##########################################"echo ...

一、目的

为简化Memcached快速安装部署,并添加至服务启动项,开机自启动。

二、脚本

2.1 github地址
github链接
2.2 脚本内容

#!/bin/bash
#mail:xuel@anchnet.com
#function:auto install memcached
clear
echo "##########################################"
echo "#       Auto Install Memcached-1.4                          ##"
echo "#       Press Ctrl + C to cancel                                 ##"
echo "#       Any key to continue                                       ##"
echo "##########################################"
read -n 1
softdir="/software"
memcached_url="http://www.danga.com/memcached/dist/memcached-1.4.0.tar.gz"
libevent_url="https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz"
sys_version=$(rpm -q centos-release|cut -d- -f3)

sys_init() {
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ ${sys_version} != "6" ] && echo "Please use centos6.x" && exit 1
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
yum install -y gcc gcc-c++ wget
}

download_software() {
if [ ! -d ${softdir} ];then
	mkdir ${softdir} && cd ${softdir}
else
	cd ${softdir}
fi
for software_url in ${memcached_url} ${libevent_url}
do
	wget -c ${software_url}
	if [ $? -eq 0 ];then
		for software in `ls`
		do
			tar zxf $software -C /tmp
		done
	fi
done
}

install() {
cd /tmp/libevent-2.1.8-stable
./configure --prefix=/usr/local/libevent
make && make install
rm -rf /tmp/libevent-2.1.8-stable 
echo "/usr/local/libevent/lib">/etc/ld.so.conf.d/libevent.conf
ldconfig

cd /tmp/memcached-1.4.0
./configure --with-libevent=/usr/local/libevent --prefix=/usr/local/memcached
make && make install
rm -rf /tmp/memcached-1.4.0
echo "export PATH=$PATH:/usr/local/memcached/bin">/etc/profile.d/memcached.sh && source /etc/profile.d/memcached.sh
}

start_server() {
cat >/etc/init.d/memcached-server<<EOF
#!/bin/bash
#auth:kaliarch
# memcached    Startup script for memcached processes
#
# chkconfig: - 90 10
# description: Memcache provides fast memory based storage.
# processname: memcached

. /etc/rc.d/init.d/functions

memcached="/usr/local/memcached/bin/memcached"
prog="memcached"
port=11211
user=nobody
mem=20
lockfile=\${LOCKFILE-/var/lock/subsys/memcached}
pidfile=\${PIDFILE-/tmp/memcached.pid}
getpid=\$(pidof memcached)
start() {
    action $"Starting \$prog: " /bin/true
    # Starting memcached with 64MB memory on port 11211 as deamon and user nobody
    \$memcached -d -m \$mem -p \$port -u \$user -P \${pidfile}

    RETVAL=$?
    [ \$RETVAL = 0 ] && touch \${lockfile}
    return \$RETVAL
}

stop() {
    if test "x\${getpid}" != x; then
        action $"Stopping \$prog " /bin/true
        killall memcached
    fi
    RETVAL=\$?
    [ \$RETVAL = 0 ] && rm -rf \${lockfile} \${pidfile}
    return \$RETVAL
}

case "\$1" in
        start)
            start
            ;;

        stop)
            stop
            ;;

        status)
	    status -p \${pidfile} \${memcached}
	    RETVAL=\$?
            ;;

        restart)
            stop
            start
            ;;
        condrestart)
            if test "x\${getpid}" != x; then
                stop
                start
            fi
            ;;

        *)
            echo $"Usage: \$0 {start|status|stop|restart|condrestart}"
            exit 1

esac

exit \${RETVAL}
EOF
cd /
chmod +x /etc/init.d/memcached-server
chkconfig memcached-server on
service memcached-server start
}

main() {
sys_init
download_software
install
start_server
}

main

三、测试


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。