启动脚本在首次启动时不运行

启动脚本在首次启动时不运行

添加到 GCP 中的实例startup-script似乎不会在第一次启动时运行(但在后续启动时会运行)

我正在使用https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-7-v20160921基础镜像,但在稍旧版本的 centos-7 镜像上也遇到了同样的问题。

无论我使用 API 还是 Web 控制台创建实例,都会发生这种情况。

根据文档(https://cloud.google.com/compute/docs/startupscript)添加带有脚本的键startup-script和值的元数据应该会导致脚本在每一个引导。

(其中有一个关于将脚本添加到正在运行的实例的部分,并且提到需要重新启动才能使脚本运行,但这(显然)不应该适用于新实例。)

可以使用sudo google_metadata_script_runner --script-type startup(强制运行脚本https://cloud.google.com/compute/docs/startupscript#rerunthescript),这意味着脚本已加载,但尚未运行,所以我不确定发生了什么。

我是不是做错了什么,或者我发现了 GCP 中的一个错误?:)

答案1

这对我来说确实有效,使用 Centos 7 镜像没有任何问题。我从控制台创建了虚拟机并添加了这个小脚本

#! /bin/bash
yum update
yum install -y httpd
cat <<EOF > /var/www/html/index.html
<html><body><h1>Hello World</h1>
<p>This page was created from a simple startup script!</p>
</body></html>
EOF

创建虚拟机后,我可以验证 Apache 是否已安装在串行控制台


Feb  3 20:36:17 instance-3 startup-script: INFO startup-script: ---> Package httpd.x86_64 0:2.4.6-45.el7.centos will be installed
Feb  3 20:36:17 instance-3 startup-script: INFO startup-script: --> Processing Dependency: httpd-tools = 2.4.6-45.el7.centos for package: httpd-2.4.6-45.el7.centos.x86_64
Feb  3 20:36:17 instance-3 startup-script: INFO startup-script: --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-45.el7.centos.x86_64
….
Feb  3 20:36:20 instance-3 startup-script: INFO startup-script: Installed:
Feb  3 20:36:20 instance-3 startup-script: INFO startup-script:   httpd.x86_64 0:2.4.6-45.el7.centos
Feb  3 20:36:20 instance-3 startup-script: INFO startup-script: Dependency Installed:
Feb  3 20:36:20 instance-3 startup-script: INFO startup-script:   apr.x86_64 0:1.4.8-3.el7                     apr-util.x86_64 0:1.5.2-6.el7
Feb  3 20:36:20 instance-3 startup-script: INFO startup-script:   httpd-tools.x86_64 0:2.4.6-45.el7.centos     mailcap.noarch 0:2.1.41-2.el7
Feb  3 20:36:20 instance-3 startup-script: INFO startup-script: Complete!

脚本确实安装了 Apache,但虚拟机中的服务尚未启动。我只需在虚拟机中使用 SSH 即可启动该服务。

答案2

这不是一个错误,它仍然存在并且很棘手。

启动脚本在所有脚本之后运行init.d

因此如果您初始化一个/etc/environment文件并尝试在脚本中使用它init.d,则在第一次启动时,/etc/environment当您的脚本启动时它将不存在。

第二次启动时,没有其他问题,/etc/environment已经存在:-)

相关内容