通过UserData执行Shell脚本无法向hosts文件追加内容求助
问题描述
尝试通过UserData在服务器启动时执行脚本,向/etc/hosts文件末尾追加IP和主机名,但未成功。具体情况:
- 服务器启动后,
Service_start.sh会执行多个命令和hostnames.sh脚本,其他脚本运行正常,仅hostnames.sh无预期效果 - 日志显示
hostnames.sh执行成功(输出两个SUCCESS),但/etc/hosts中没有目标IP和主机名 - 手动运行
hostnames.sh脚本可正常修改/etc/hosts
相关脚本内容:
Service_start.sh
#!/bin/bash #script to udate hostnames in hosts file ip=`facter testip` hostname=`facter testhostname` sudo /bin/bash /opt/resources/hostnames.sh >> /opt/test.log
hostnames.sh
#!/bin/bash #script to udate hostnames in hosts file ip=`facter testip` hostname=`facter testhostname` echo " " >> /etc/hosts if [ $? -eq 0 ]; then echo SUCCESS else echo FAIL fi echo "$ip $hostname" >> /etc/hosts if [ $? -eq 0 ]; then echo SUCCESS else echo FAIL fi
排查与修复步骤
1. 先确认变量是否为空
自动执行时最可能的问题是facter testip和facter testhostname未返回有效值,导致写入/etc/hosts的内容为空。修改hostnames.sh添加调试日志,确认变量状态:
#!/bin/bash #script to udate hostnames in hosts file ip=`facter testip` hostname=`facter testhostname` # 新增:打印变量值到日志 echo "DEBUG: ip=$ip, hostname=$hostname" >> /opt/test.log echo " " >> /etc/hosts if [ $? -eq 0 ]; then echo SUCCESS else echo FAIL fi echo "$ip $hostname" >> /etc/hosts if [ $? -eq 0 ]; then echo SUCCESS else echo FAIL fi
重启服务器后查看/opt/test.log,如果ip或hostname为空,说明facter变量在脚本执行时还未初始化。
2. 等待facter变量就绪
如果是变量未初始化,在Service_start.sh中添加等待逻辑,直到变量有值再执行后续操作:
#!/bin/bash #script to udate hostnames in hosts file # 循环等待facter变量生成 while true; do ip=$(facter testip) hostname=$(facter testhostname) if [ -n "$ip" ] && [ -n "$hostname" ]; then break fi echo "Waiting for facter variables..." >> /opt/test.log sleep 5 done sudo /bin/bash /opt/resources/hostnames.sh >> /opt/test.log
3. 直接传递变量避免环境依赖
不在hostnames.sh中重复调用facter,而是从Service_start.sh把已获取的变量传递过去,规避sudo环境差异问题:
修改Service_start.sh:
#!/bin/bash #script to udate hostnames in hosts file ip=$(facter testip) hostname=$(facter testhostname) # 通过参数传递变量给hostnames.sh sudo /bin/bash /opt/resources/hostnames.sh "$ip" "$hostname" >> /opt/test.log
修改hostnames.sh:
#!/bin/bash #script to udate hostnames in hosts file # 从参数获取变量 ip=$1 hostname=$2 echo "DEBUG: ip=$ip, hostname=$hostname" >> /opt/test.log echo " " >> /etc/hosts if [ $? -eq 0 ]; then echo SUCCESS else echo FAIL fi echo "$ip $hostname" >> /etc/hosts if [ $? -eq 0 ]; then echo SUCCESS else echo FAIL fi
4. 确保sudo保留环境变量(可选)
如果facter依赖特定环境变量,执行sudo时添加-E参数保留当前环境:
sudo -E /bin/bash /opt/resources/hostnames.sh >> /opt/test.log
内容的提问来源于stack exchange,提问作者Sathya
相关产品推荐
相关产品推荐

