#!/bin/bash

file_name="/sdzw/es5/conf/es.config"

#安装目录

install_dir="/es5/esinstall"

#es数据目录

es_dir="/data/es5data"

network_host=`cat ${file_name} | grep network_host`

network_host=${network_host#*=}

#节点名

node_name="\"$network_host\""

hosts=`cat ${file_name} | grep hosts`

hosts=${hostst#*=}

if[ $1 ]

then

jvm_options=$1

else

jvm_options="8g"

fi

jarpath="/es5/source"

mkdir_cmd="mkdir -p ${install_dir}"

sed -i 'a\fs.file-max=65536'   /etc/sysctl.conf

sysctl -p

#修改环境变量

sed -i '$a\export NODE_HOME=/es5/esinstall/node-v6.9.1-l-linux-x64'  /etc/profile

sed -i '$a\export PATH=$PATH:$NODE_HOME/bin'   /etc/profile

source /etc/profile

userdel es5

adduser es5

if [ -f ${es_dir} ]

then

echo "数据目录已存在"

else

mkdir -p ${es_dir}

fi

chmod 777 /data/es5data

if [ -f ${install_dir} ]

then

echo "安装目录已存在"

else

mkdir_cmd

fi

chmod es5:es5 -R ${install_dir}

tar -xzvf ${jarpath}/elasticsearch-5.4.0.tar.gz -C  ${install_dir}

tar -xzvf ${jarpath}/eshead.tar.gz -C  ${install_dir}

tar -xzvf ${jarpath}/node.tar.gz -C  ${install_dir}

chown es5:es5 -R {install_dir}/elasticsearch-5.4.0

chown es5:es5 -R {install_dir}/elasticsearch-head

chown es5:es5 -R {install_dir}/node-v6.9.1-linux-x64

#修改配置文件

sed -i "/#node.name: node-1/a\node.name: ${node_name}" ${install_dir}/elasticsearch-5.4.0/config/elasticsearch.yml

sed -i "s/-Xms2g/-Xms${ jvm_options}/g"  ${install_dir}/elasticsearch-5.4.0/config/jvm.options

sed -i "s/-Xmx2g/-Xmx${ jvm_options}/g"  ${install_dir}/elasticsearch-5.4.0/config/jvm.options

cat >> ${install_dir}/elasticsearch-5.4.0/config/elasticsearch.yml

discovery.zen.ping.unicast.hosts: ${hosts}

network.host: ${network_host}

EOF

echo '注册系统服务'

cp -af /es5/conf/esserver /etc/init.d/esserver

chmod +x /etc/init.d/esserver

chkconfig_list=`chkconfig --list 2>&1`

if echo "$chkconfig_list" | grep -q "esserver" ; then

chkconfig --del esserver

fi

chkconfig --add esserver

echo "启动es"

su es5 -c "nohup /es5/esinstall/elasticsearch-5.4.0/bin/elasticsearch >> /dev/null &"

echo "es系统服务注册成功"

05-28 02:38