因为 tomcat 在启动的时候需要在 /etc/hosts 里面添加下主机名的映射关系,但是如果服务器多的话,手工一台台去填写就不现实了,所以就用脚本批量处理下。
正确的版本:
HOSTNAME=`hostname`
ip_addr=`/sbin/ifconfig bond0 |grep -a "inet addr:" |awk -F":" '{print $2}' |egrep -o '([0-9]{1,3}\.?){4}'`
echo ${ip_addr} > temp.txt
sudo sh -c 'echo "`cat temp.txt` ${HOSTNAME}" >> /etc/hosts'
有问题的版本:
#!/bin/bash
HOSTNAME=`hostname`
ip_addr=`/sbin/ifconfig bond0 |grep -a "inet addr:" |awk -F":" '{print $2}' |egrep -o '([0-9]{1,3}\.?){4}'`
sudo sh -c 'echo "${ip_addr} ${HOSTNAME}" >> /etc/hosts'
有问题的版本输出的结果是,只会在 /etc/hosts 文件中输入主机名以及一个空格,想不明白,那个 ip_addr 变量绝对是有值的。
本文介绍了一种用于批量配置Tomcat启动时所需的主机名映射的方法。通过编写Shell脚本自动化处理多台服务器上的/etc/hosts文件更新,避免手动操作的繁琐与错误。
564

被折叠的 条评论
为什么被折叠?



