mobcdi
mobcdi

Reputation: 1592

Google Compute Engine Startup Script: Not running on startup

I'm trying to get the following script to run on a CentOS instance at start up on GCE. I have the custom metadata "startup-script" set on the instance name and the following script as the value.

The script isn't executing on startup, reboot or when I run /usr/share/google/run-startup-scripts but does execute if I create it locally on the instance and execute there

What obvious thing am I missing?

#! /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

Upvotes: 6

Views: 5420

Answers (3)

Wojtek_B
Wojtek_B

Reputation: 4443

I was curious how's the situation with this today (6 years after original post) so I've created three VM's with CentOS 6,7 & 8 and put the startup script in place.

I used the script from the initial questions without modifications.

After VM's were created in all three cases httpd was installed and was running. Partial serial console outputs below.

Results for CentOS 6:

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

Results for 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

Results for 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

Whatever was causing the script not to run at the time this question was asked it is not the case now.

Upvotes: 1

Yao Li
Yao Li

Reputation: 2256

I used CentOS 7 as base image and install some libraries but it doesn't work. After switch to CentOS 6 and it works well (seems CentOS 7 has issue).

Upvotes: 0

biofoo
biofoo

Reputation: 11

My personal experience was facing with two issues:

1) Commands that needed interaction, would not run properly on startup. For examples, apt-get install asks you to confirm the process (Y/n)? In that case, you can turn off interaction and pass "yes" by replacing

yum install foo
apt-get install foo

with

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

Also, if you're using Debian, the following before any command would suppress the need for interaction:

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

2) The other obvious issue is sometimes you have to wait for the process to finish, and that may happen much later than your instance shows up as "running".

Upvotes: 0

Related Questions