【问题标题】:If I run the script from a command line (as root), it works. If it runs at a boot time, it fails如果我从命令行(以 root 身份)运行脚本,它就可以工作。如果它在启动时运行,它会失败
【发布时间】:2016-12-02 09:50:07
【问题描述】:

我有一个要在启动时运行的 shell 脚本。在这个脚本中,我使用 aws cli 将我的 IP 更新为 route53 记录集,如果我从命令行(作为 root 用户)运行脚本,它可以工作。如果它在引导时运行,它将失败。我已经为 root 用户配置了 aws 凭证。 我的 update-route53-record.sh

#!/bin/bash

DOMAIN_NAME="mydomain.com"
HOSTED_ZONE_ID="Z1986XXXXXXXXX"

main(){
    UPDATED_IP_LIST=$(updated_ip_list)    
    update_route53_record "$UPDATED_IP_LIST"
}

# Get IP List of $DOMAIN_NAME from route53
get_ip_list(){
    RECORD_SET_JSON=$( aws route53 list-resource-record-sets --hosted-zone-id Z1986QIYBBYSUJ --query "ResourceRecordSets[?Name == '$DOMAIN_NAME.']")

    #Remove the first and last character in string  to convert json array to json object
    RECORD_SET_JSON=${RECORD_SET_JSON:1:-1}

    # Need to install jq to parse json http://xmodulo.com/how-to-parse-json-string-via-command-line-on-linux.html
    # Get value of ResourceRecords
    RECORD_SET_JSON=$( echo $RECORD_SET_JSON | jq -r '.ResourceRecords' )

    echo $RECORD_SET_JSON
}

updated_ip_list(){
    # Get public IP of running instance
    IP=$( curl http://169.254.169.254/latest/meta-data/public-ipv4 )
    # IP="192.168.10.1"

    # Get IP list from Route53 by invoking get_ip_list
    IP_LIST=$(get_ip_list)    

    # Get length of json array
    LENGTH=$(echo $IP_LIST | jq '. | length')

    # Add one element to last array
    IP_LIST=$(echo $IP_LIST | jq '.['$LENGTH'].Value |= .+ '\"$IP\"'')

    echo $IP_LIST
}

update_route53_record(){   
    JSON_REQUEST='{
              "Comment": "Update the A record set",
              "Changes": [
                {
                  "Action": "UPSERT",
                  "ResourceRecordSet": {
                    "Name": '\"$DOMAIN_NAME\"',
                    "Type": "A",
                    "TTL": 300,
                    "ResourceRecords": '$1'
                  }
                }
              ]
            }'
    #echo $JSON_REQUEST
    #echo "Calling API..."
    aws route53 change-resource-record-sets  --hosted-zone-id "$HOSTED_ZONE_ID" --change-batch "$JSON_REQUEST"

    # write to log file
    echo "aws route53 change-resource-record-sets  --hosted-zone-id \"$HOSTED_ZONE_ID\" --change-batch \"$JSON_REQUEST\"" | sudo tee /var/log/update-route53.log
}
main
exit 0

我安装我的脚本在启动时运行

sudo cp update-route53-record.sh /etc/init.d
sudo chmod +x /etc/init.d/update-route53-record.sh
sudo update-rc.d update-route53-record.sh defaults 98 02

但如果我从命令行(以 root 身份)运行脚本,它就可以工作。如果它在引导时运行,它将失败。我已经为 root 用户配置了 aws 凭证。 你能帮忙吗?

【问题讨论】:

  • 169.范围通常是“无法获取IP”类型的地址。我猜你是在尝试在网卡驱动程序完全加载之前运行脚本吗?
  • @SteveMatthews 那么如何在启动时运行脚本,但在完全加载网卡驱动程序之后呢?
  • 您也许可以在脚本的开头添加一个睡眠,以便它开始但然后等待?
  • @SteveMatthews 它能够获得 IP。调试的时候可以打印出IP。

标签: shell amazon-web-services ubuntu-14.04 aws-cli


【解决方案1】:

这可能是因为您在用户数据中使用了sudo。 AWS 以 root 身份运行这些命令,并且使用 sudo 将不起作用,因为 sudo 需要 tty。

来自AWS Documentation

作为用户数据输入的脚本以 root 用户身份执行,因此请勿在脚本中使用 sudo 命令。

尝试删除 sudo 看看效果如何。

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 2021-02-26
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多