Bash EC2用户数据脚本.sh文件和手动执行不同

Bash EC2用户数据脚本.sh文件和手动执行不同,bash,amazon-web-services,amazon-ec2,user-data,Bash,Amazon Web Services,Amazon Ec2,User Data,我正在尝试执行以下用户数据脚本 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 Ido查看/var/log/cloud ini

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

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


Ido查看
/var/log/cloud init output.sh中的控制台,但当作为userdata脚本的一部分运行时,它似乎不会创建目录。

您在shell脚本中调用shell脚本,因此需要确保:

  • 将此作为ec2用户数据脚本的第一行/bin/bash
  • 使用
    source
    命令(别名是
    )调用
    myFile.sh
    ,如下所示:
    /opt/myFile.sh
    因此它将运行myFile脚本


  • 注意:ec2用户数据脚本以root用户身份运行,因此每次运行命令时不需要使用
    sudo

    在shell脚本中调用shell脚本,因此需要确保:

  • 将此作为ec2用户数据脚本的第一行/bin/bash

  • 使用
    source
    命令(别名是
    )调用
    myFile.sh
    ,如下所示:
    /opt/myFile.sh
    因此它将运行myFile脚本


  • 注意:ec2用户数据脚本以root用户身份运行,因此每次运行命令时无需使用
    sudo

    解决方案:首先将CD放入目录

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

    解决方案:先将CD放入目录

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

    /var/log/cloud init output.log中的任何相关内容?同意/var/log/cloud init output.log中的任何相关内容。请参见:#上的示例用户数据脚本/bin/bash wget-P/opt tar-xvf/opt/myFile.tar-C/opt./opt/mySH.sh仍在提取。。。输出证明它已运行,但未创建目录。是否有类似
    的空间/opt/mySH.sh
    如果您不确定,请使用
    寻源/opt/mySH.sh
    是,在有空格和没有空格的情况下尝试。我在用亚马逊AMI,如果有关系的话
    -rwxr-xr-x 1 root根147241898 May 30 12:01 mySH.sh
    /var/log/cloud init output.log
    /var/lib/cloud/instance/scripts/part-001:第9行:。:/opt mySH.sh:无法执行二进制文件
    问题在于脚本本身。直接从实例运行脚本时,能否确认脚本是否正常?同意。请参见:#上的示例用户数据脚本/bin/bash wget-P/opt tar-xvf/opt/myFile.tar-C/opt./opt/mySH.sh仍在提取。。。输出证明它已运行,但未创建目录。是否有类似
    的空间/opt/mySH.sh
    如果您不确定,请使用
    寻源/opt/mySH.sh
    是,在有空格和没有空格的情况下尝试。我在用亚马逊AMI,如果有关系的话
    -rwxr-xr-x 1 root 147241898 May 30 12:01 mySH.sh
    /var/log/cloud init output.log
    /var/lib/cloud/instance/scripts/part-001:第9行:。:/opt mySH.sh:无法执行二进制文件
    问题在于脚本本身。当您直接从实例运行脚本时,是否可以确认脚本是否正常工作?