【问题标题】:Google Compute Engine Startup Script: Not running on startupGoogle Compute Engine 启动脚本:启动时未运行
【发布时间】:2014-03-05 15:52:53
【问题描述】:

我试图让以下脚本在 GCE 上启动时在 CentOS 实例上运行。 我在实例名称上设置了自定义元数据“启动脚本”,并将以下脚本作为值。

脚本在启动、重启或运行 /usr/share/google/run-startup-scripts 时不会执行,但如果我在实例上本地创建它并在那里执行,则会执行

我遗漏了什么明显的东西?

#! /bin/bash
# Installs apache and a custom homepage
# 1234567 123456

#Get this servers name from the metadata server and use the header to authenticate
THIS_SERVER_NAME=$(curl http://metadata/computeMetadata/v1/instance/hostname -H "X-Google-Metadata-Request: True")
#Turn off IPtables firewall that comes installed
service iptables stop
#Install apache
yum install -y httpd
#start apache
service httpd start
#create custom default homepage for this server
cat <<EOF > /var/www/html/index.html
<html><body><h1>Hello World</h1>
<p>This is Server: $THIS_SERVER_NAME</p>
</body></html>
EOF

【问题讨论】:

  • 我还尝试从存储桶中加载脚本,该存储桶至少在 /var/log/google/log 中向我显示了脚本已下载,但它实际上并未执行任务。我需要从 gcutil 设置权限吗?
  • 还在 gcutil 命令中添加了 --service_account_scopes=storage-ro 但仍然没有乐趣
  • 我遇到了类似的问题,但是我的脚本在我重新启动后启动,但不是在创建实例时启动....
  • 这里有同样的问题,但即使重启选项也不起作用
  • 如果有人仍然遇到此问题: 1. 在脚本开头包含“touch hello”命令,并检查文件是否在重新启动后在您的主目录中创建。如果是,则脚本实际上已执行,但由于脚本中的错误而停止。 2. 查看串行日志(可通过 Cloud Console 访问)并在那里搜索“启动”,这可能会为您提供更多关于出现问题的提示。 3. 在 Ubuntu 上,目前有一个错误会阻止启动脚本工作:github.com/GoogleCloudPlatform/compute-image-packages/issues/…

标签: startup google-compute-engine


【解决方案1】:

我的个人经历遇到了两个问题:

1) 需要交互的命令在启动时无法正常运行。例如,apt-get install 要求您确认进程(Y/n)?在这种情况下,您可以关闭交互并通过替换传递“是”

yum install foo
apt-get install foo

yum -y --assumeyes install foo
apt-get -y --force-yes install foo

另外,如果您使用的是 Debian,在任何命令之前的以下内容都会抑制交互的需要:

sudo DEBIAN_FRONTEND=noninteractive <your command here, e.g., apt-get -y install foo>

2) 另一个明显的问题是有时您必须等待进程完成,而这可能比您的实例显示为“正在运行”要晚得多。

【讨论】:

    【解决方案2】:

    我使用 CentOS 7 作为基础映像并安装了一些库,但它不起作用。切换到 CentOS 6 后,它运行良好(似乎 CentOS 7 有问题)。

    【讨论】:

      【解决方案3】:

      我很好奇今天的情况如何(原帖发布 6 年后),所以我用 CentOS 6,7 和 8 创建了三个 VM,并将启动脚本放置到位。 p>

      我使用了最初问题中的脚本,没有修改。

      在所有三种情况下创建 VM 后,httpd 已安装并正在运行。部分serial console 输出如下。

      CentOS 6 的结果:

      Installed:
      httpd.x86_64 0:2.2.15-69.el6.centos
      ...
      Complete!                             
      Starting httpd: [  OK  ]
      exit status 0
      

      CentOS 7 的结果:

      Installed:
      httpd.x86_64 0:2.4.6-93.el7.centos
      ...
      Complete!
      centos7 systemd: Starting The Apache HTTP Server...
      centos7 systemd: Started The Apache HTTP Server.
      GCEMetadataScripts: startup-script exit status 0
      

      CentOS 8 的结果:

      Installing:
      httpd            x86_64 2.4.37-21.module_el8.2.0+494+1df74eae
      ...
      systemd[1]: Starting The Apache HTTP Server...
      systemd[1]: Started The Apache HTTP Server.
      GCEMetadataScripts: startup-script exit status 0
      

      在提出这个问题时,无论是什么导致脚本无法运行,现在都不是这种情况。

      【讨论】: