gpt4 book ai didi

amazon-web-services - 如何在启动时为 ubuntu server 18.04 运行命令?

转载 作者:行者123 更新时间:2023-12-04 18:35:36 25 4
gpt4 key购买 nike

我想在启动 EC2 实例时运行命令。实例的 AMI 为 Ubuntu Server 18.04 LTS (HVM), SSD Volume Type .

以下是我尝试过的方法:

  • 创建一个名为 test.sh 的脚本在 /etc/init.d/ 下.
  • sudo chmod +x /etc/init.d/test.sh
  • sudo chown root.root /etc/init.d/test.sh
  • sudo update-rc.d test.sh defaults
  • 停止 EC2 实例。
  • 启动 EC2 实例。

  • 这是 test.sh的内容:
    #!/bin/bash

    echo hello > /home/ubuntu/test.log

    我预计我会看到 test.log /home/ubuntu 下的文件.但是我找不到文件。

    我也试过添加 echo hello > /home/ubuntu/test.log/etc/rc.local文件。但这不起作用,八岁。

    我尝试过的另一种方法是添加 echo hello > /home/ubuntu/test.log到用户数据。这也行不通。

    enter image description here

    我发现一行说 2019-05-13 03:23:18,162 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'b'echo hello > /home/ubunt'...'/var/log/cloud-init.log .但这只是一个警告,而不是错误消息。

    我的配置可能有什么问题?

    EC2 上是否有任何错误日志?

    最佳答案

    我关注了这个 document在启动时成功运行命令。

    我将脚本放在用户数据中。这是我的用户数据:

    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0

    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"

    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]

    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"

    #!/bin/bash
    /bin/echo hello > /home/ubuntu/test.log
    --//
    /bin/echo hello > /home/ubuntu/test.log将在每次启动时触发。这是因为 SCRIPTS-USER设置为 ALWAYS .

    关于amazon-web-services - 如何在启动时为 ubuntu server 18.04 运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099657/

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