gpt4 book ai didi

bash - EC2 用户数据,启动时未执行的特定命令,手动工作正常

转载 作者:行者123 更新时间:2023-11-29 09:27:12 25 4
gpt4 key购买 nike

我在提交之前搜索过这个,但找不到任何东西。

我有一个与我的 EC2 实例 (ubuntu AMI) 一起运行的 bash 脚本最后一条命令将 IP 地址保存为一个变量——这似乎“突然”(最近阅读)停止了工作。就好像它完全忽略了命令。

如果我手动复制粘贴命令,它可以正常工作(但这违背了这一点)

脚本

#!/bin/bash

# update the install on the ubuntu image
sudo apt-get update -y

# install Docker's certificate etc
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

# add dockers official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# set up the stable repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

# update again now with added repo
sudo apt-get update -y

# Install Docker CE
sudo apt-get install docker-ce -y

# install docker-compose (in case you need to make a docker-compose file)
sudo apt-get install docker-compose -y

# make docker group if not already created
sudo groupadd docker

# make ubuntu user part of docker group
sudo usermod -aG docker ubuntu

# create results dir
mkdir /home/ubuntu/mount

# change permissions on mount drive so all users can read/write
sudo chmod 775 /home/ubuntu/mount

# change ownership of mount folder so that files can be sent to it
sudo chown -R ubuntu /home/ubuntu/mount/

# install htop
sudo apt install htop

#set ip as env var
IP=$(hostname -i)

这不会将 IP 地址保存为变量,就像我说的那样,如果我事后手动将它复制到命令行中就可以了。

我在 AWS 上的 EC2 控制台中运行它。在“高级详细信息”中的“用户数据”部分下。

似乎发生的是以下命令未运行:

IP=$(hostname -i)

我通过 ssh 连接到实例并调用“echo $IP”但没有得到任何结果。但是,如果我手动运行“IP=$(hostname -i)”命令,它会使用当前 IP 地址填充变量。

更新

我现在已将脚本移至 GitHub:https://raw.githubusercontent.com/ScreamingJoypad/jmeter-armada/master/userdata.sh

在实例上单独运行每个命令都可以正常工作,创建实例后从实例中创建脚本也是如此

但是使用 curl -o <filename>.sh <link>然后运行 ​​. <filename>.sh不起作用并提供以下错误:

enter image description here

最佳答案

只是帮助您入门。

在一个 shell 中创建的变量在另一个 shell 中不可用。

可以启动一个新的 shell,通过在它启动的地方使用 export 来保存它。

help export

可以使用源代码在当前 shell 中运行脚本。

help source

可以在登录时自动运行命令并设置变量。

man bash #look for 'bashrc'.

关于bash - EC2 用户数据,启动时未执行的特定命令,手动工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49530783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com