AWS实例启动时自动执行脚本

修改每个实例的用户数据

实例 => 操作 => 实例设置 => 编辑用户数据

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 World" >> /tmp/testfile.txt
--//--

- [scripts-user, always]:重启也执行一遍这个脚本

/bin/echo "Hello World" >> /tmp/testfile.txt :此部分可以替换成你自己的脚本代码

注意:这个脚本任务是使用的root用户执行的,所以需要注意shell代码里 ~ 的路径指向

参考:

在 EC2 实例初始启动之后运行用户数据 | AWS re:Post

Run commands on your Linux instance at launch - Amazon Elastic Compute Cloud

在亚马逊 AWS 上重新启动 EC2 实例后,读取脚本执行自动配置的问题。_aws userdata重启实例会执行吗_茶儿啤酒的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/GrimRaider/article/details/131461571