AWS EC2 用户数据创建 SH 脚本

AWS EC2 User Data create SH script

我正在 AWS 中创建一个新实例并添加一些用户数据,但部分工作是创建一个 sh 文件然后执行。

我正在尝试:

#!/bin/bash -x
cd /tmp
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
sudo wget -O ethminer.tar.gz https://github.com/ethereum-mining/ethminer/releases/download/v0.18.0/ethminer-0.18.0-cuda-9-linux-x86_64.tar.gz
sudo tar xvfz ethminer.tar.gz
cd bin
cat > runner.sh << __EOF__
#!/bin/bash -x
SERVERS=(us1 us2 eu1 asia1)
while (true); do
  PREFERRED_SERVER=${!SERVERS[${!RANDOM} % ${!#SERVERS[@]}]}
  ./ethminer \
    -P stratums://xxx.${!INSTANCE_ID}@${!PREFERRED_SERVER}.ethermine.org:5555 \
    -P stratums://xxx.${!INSTANCE_ID}@us1.ethermine.org:5555 \
    -P stratums://xxx.${!INSTANCE_ID}@us2.ethermine.org:5555 \
    -P stratums://xxx.${!INSTANCE_ID}@eu1.ethermine.org:5555 \
    -P stratums://xxx.${!INSTANCE_ID}@asia1.ethermine.org:5555 \
  >> /tmp/ethminer.log 2>&1
done
__EOF__
sudo chmod +x runner.sh
sudo nohup ./runner.sh &

除 sh 外一切正常,我的命令创建了 runner.sh 脚本,但它是空的。

UserData 不起作用,因为它是从 CloudFormation 设计的,因此它的语法不正确,无法在独立实例中使用。具有正确语法的脚本如下,它将生成您的 runner.sh。我没有测试跑步者的功能,只测试了 runner.sh.

的创建
#!/bin/bash -x
cd /tmp
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
sudo wget -O ethminer.tar.gz https://github.com/ethereum-mining/ethminer/releases/download/v0.18.0/ethminer-0.18.0-cuda-9-linux-x86_64.tar.gz
sudo tar xvfz ethminer.tar.gz
cd bin
cat > runner.sh << __EOF__
#!/bin/bash -x
SERVERS=(us1 us2 eu1 asia1)
while (true); do
  PREFERRED_SERVER=${SERVERS[${RANDOM} % ${#SERVERS[@]}]}
  ./ethminer \
    -P stratums://xXX.${insTANCE_ID}@${PREFERRED_SERVER}.ethermine.org:5555 \
    -P stratums://xxx.${INSTANCE_ID}@us1.ethermine.org:5555 \
    -P stratums://xxx.${INSTANCE_ID}@us2.ethermine.org:5555 \
    -P stratums://xxx.${INSTANCE_ID}@eu1.ethermine.org:5555 \
    -P stratums://xxx.${INSTANCE_ID}@asia1.ethermine.org:5555 \
  >> /tmp/ethminer.log 2>&1
done
__EOF__
sudo chmod +x runner.sh
sudo nohup ./runner.sh &