6
votes

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
3
I also tried loading the script from a bucket which at least shows me in /var/log/google/log the script is downloaded but it still doesn't actually carry out the tasks. Is there permissions I need to set from gcutil?mobcdi
Also added --service_account_scopes=storage-ro to the gcutil command but still no joymobcdi
I'm having a similar problem, but my script starts after I reboot, but not when the instance is created....brdido
having the same problema here, but even the reboot option doesn't workMaviles
If anyone is still experiencing this issue: 1. include a "touch hello" command at the beginning of your script and check if the file was created in your home directory after a reboot. If yes, the script is actually executed but stops due to an error in your script. 2. look at the serial logs (accessible via the Cloud Console) and search for "startup" there, which might give you more hints on what's going wrong. 3. on Ubuntu there is currently a bug stopping startup-scripts from working: github.com/GoogleCloudPlatform/compute-image-packages/issues/…TomTasche

3 Answers

0
votes

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".

0
votes

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).

0
votes

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.