Startup script doesn't seem to work

Viewed 7659

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!

4 Answers

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'

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.

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.

Related