【问题标题】:EC2 User Data Script .sh file and Manual Execute DifferEC2 用户数据脚本 .sh 文件和手动执行不同
【发布时间】:2017-07-19 16:45:44
【问题描述】:

我正在尝试执行以下用户数据脚本

sudo wget https://files.mysite.com/downloads/myFile.tar -P /opt
sudo tar -xvf /opt/myTar.tar -C /opt
sudo /opt/myFile.sh

当我手动执行 .sh 文件时,我看到:

Extracting...
Unpacking...
Cleaning up...
Complete

它会在/opt/myDirectory中创建一个目录

确实/var/log/cloud-init-output.sh 中看到了控制台,但它没有在作为用户数据脚本的一部分运行时似乎创建了目录。

【问题讨论】:

  • /var/log/cloud-init-output.log 中有什么相关的吗?

标签: bash amazon-web-services amazon-ec2 user-data


【解决方案1】:

你在一个 shell 脚本中调用一个 shell 脚本,所以你需要确保:

  1. 将此作为您的 ec2 用户数据脚本的第一行 #!/bin/bash

  2. 使用source 命令(别名为.)调用myFile.sh,如下所示:. /opt/myFile.sh,这样它将运行 myFile 脚本

注意:ec2 用户数据脚本以 root 身份运行,因此您无需在每次运行命令时都拥有sudo

【讨论】:

  • 同意。查看示例用户数据脚本:Running Commands on Your Linux Instance at Launch
  • #!/bin/bash wget example.com/myFile.tar -P /opt tar -xvf /opt/myFile.tar -C /opt ./opt/mySH.sh 仍然得到 Extracting... 输出证明它运行了,但没有创建目录。
  • 你有没有. /opt/mySH.sh这样的空间如果你不确定,使用sourcing /opt/mySH.sh
  • 是的,尝试了有无空格。如果重要,我正在使用 Amazon AMI? -rwxr-xr-x 1 root root 147241898 May 30 12:01 mySH.sh/var/log/cloud-init-output.log/var/lib/cloud/instance/scripts/part-001: line 9: .: /opt/MySH.sh: cannot execute binary file
  • 问题在于脚本本身,当您直接从实例运行它时,您能否确认它是否正常工作?
【解决方案2】:

解决方法:先将CD放入目录。

#!/bin/bash
cd /opt
wget https://example.com/myTAR.tar
tar -xvf myTAR.tar
/opt/mySH.sh

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 2014-02-15
    • 2013-07-21
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多