然后我想在启动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
添加到用户数据。这也不起作用。
我发现一行话
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上是否还有任何错误日志?
答案 0 :(得分:1)
另一种方法是创建一个/etc/rc.local文件。 Ubuntu 18.04默认不包含一个,但是如果存在(并且由执行位/标志设置),它将执行它。有关更多信息,请访问https://vpsfix.com/community/server-administration/no-etc-rc-local-file-on-ubuntu-18-04-heres-what-to-do/
答案 1 :(得分:0)
而不是使用此命令:
sudo update-rc.d /etc/init.d/test.sh defaults
使用以下内容:
sudo update-rc.d test.sh defaults
该文件位于/etc/init.d
下是隐式的,因此,作为参数传递给命令的任何路径都将解释为相对于该文件夹的路径。
答案 2 :(得分:0)
我遵循了这个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
。