Brian Walker
Brian Walker

Reputation: 101

Startup script doesn't seem to work

I've recently started using Google's Compute engine for some of my projects the problem is my startup script doesn't seem to work, For some reason my script just doesn't work, the VM has the startup-script metadata and it works fine when I run it manually with:

sudo google_metadata_script_runner --script-type startup

Here is what I am trying to run on startup:

#!/bin/bash
sudo apt-get update
sudo rm -f Eve.jar
sudo rm -f GameServerStatus.jar
wget <URL>/Eve.jar
wget <URL>/GameServerStatus.jar
sudo chmod 7777 Eve.jar
sudo chmod 7777 GameServerStatus.jar
screen -dmS Eve sh Eve.sh
screen -dmS PWISS sh GameServerStatus.sh

There are no errors in the log either, it just seems to stop at the chmod or screen commands, Any ideas?

Thanks!

Upvotes: 10

Views: 11096

Answers (4)

yspreen
yspreen

Reputation: 1971

To add to kangbu's answer:

Checking the logs in container-optimized OS by

sudo journalctl -u google-startup-scripts.service

showed that the script could not find the user. After a long time of debugging I finally added a delay before the sudo and now it works. Seems the user is not registered when the script runs.

#! /bin/bash

sleep 10  # wait...
cut -d: -f1 /etc/passwd > /home/user/users.txt  # make sure the user exists
cd /home/user/project  # cd does not work after sudo, do it before
sudo -u user bash -c '\
source /home/user/.bashrc && \
<your-task> && \
date > /home/user/startup.log'

Upvotes: 15

Martin Zeitler
Martin Zeitler

Reputation: 76569

think that you do not need sudo, as well as the chmod 7777 should be 777

also a cd (or at least a pwd) at the beginning might be useful.

... log to text file, in order to know where the script may fail.

Upvotes: 0

kangbu
kangbu

Reputation: 196

I have the same problem @Brina mentioned. I set up metadata key startup-script and value like:

touch a
ls -al > test.txt

When I ran the script above sudo google_metadata_script_runner --script-type startup, it worked perfectly, However if I reset my VM instance the startup script didn't work. So, I checked startup script logs

...
Jul  3 04:30:37 kbot-6 ntpd[1514]: Listen normally on 5 eth0 fe80::4001:aff:fe8c:7 UDP 123
Jul  3 04:30:37 kbot-6 ntpd[1514]: peers refreshed
Jul  3 04:30:37 kbot-6 ntpd[1514]: Listening on routing socket on fd #22 for interface updates
Jul  3 04:30:38 kbot-6 startup-script: INFO Starting startup scripts.
Jul  3 04:30:38 kbot-6 startup-script: INFO Found startup-script in metadata.
Jul  3 04:30:38 kbot-6 startup-script: INFO startup-script: Return code 0.
Jul  3 04:30:38 kbot-6 startup-script: INFO Finished running startup scripts.

Yes. they found startup-script and ran it. I guessed it had executed as an another user. I changed my script like this:

pwd > /tmp/pwd.txt
whoami > /tmp/whoami.txt

The result is:

myuserid@kbot-6:/tmp$ cat pwd.txt whoami.txt
/
root

Yes. It was executed at the / diectory as root user. Finally, I changed my script to sudo -u myuserid bash -c ... which run it by specified userid.

Upvotes: 3

Ramana V V K
Ramana V V K

Reputation: 1251

  1. Go to the VM instances page.
  2. Click on the instance for which you want to add a startup script.
  3. Click the Edit button at the top of the page.
  4. Under Custom metadata, click Add item.
  5. Add your startup script using one of the following keys:

startup-script: Supply the startup script contents directly with this key. startup-script-URL: Supply a Google Cloud Storage URL to the start script file with this key.

It is working. The documentation for the new instance and existing instance as shown in GCE Start Up Script

Startup script output is written to the following log files:

  1. CentOS and RHEL: /var/log/messages
  2. Debian: /var/log/daemon.log
  3. Ubuntu 14.04, 16.04, and 16.10: /var/log/syslog

On Ubuntu 12.04, SLES 11 and 12, and all images older than v20160606:

sudo /usr/share/google/run-startup-scripts

Upvotes: 2

Related Questions