Startup script doesn't seem to work

gcp startup script
gcp startup script not running
gcp startup script logs
gke startup-script
google compute metadata startup script
terraform metadata startup-script
configure a firewall and a startup script with deployment manager
startup scripts

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!

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'

startup-script doesn't run on first boot, This actually worked for me without issues using the Centos 7 image. I created the VM from the console and added this small script #! /bin/bash yum update yum​  In GCP Console, specify a startup script directly in the Startup script section: In the GCP Console, go to the VM Instances page. Go to the VM Instances page. Click Create instance . On the Create a new instance page, fill in the properties for your instance. In the Automation section, supply

  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

command line - Script doesn't run at startup no matter what, Certain types of commands and scripts almost by definition break, if they run too early in the log in process. A few examples are: keyboard- and  I used another script to launch the main script with an output to a log file, and while the script runs, it gets Access Denied when it tries to launch it's exe on the network share. To confirm this was the problem, I moved the Updates folder and exe to a local folder on the machine, and then edited local policy to have the startup script.

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.

[SOLVED] GPO: Computer Startup Script not working with Wifi, It is reproducible, booting up with ethernet cable it works, booting up with wifi it doesn't. Checking the system logs, it seems like the startup script is applying  Running Custom Scripts at Startup Along with running programs at log on, you can also use Task Scheduler or the Startup folder to run custom batch scripts. Batch files contain commands that would normally be run in a command-line window.

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.

Debian startup script doesn't start, It looks like you're mixing all three init systems: sysVinit /etc/init.d/daemon start/​stop/restart; upstart service daemon start; systemd systemsctl  User data shell scripts must start with the #! characters and the path to the interpreter you want to read the script (commonly /bin/bash). For a great introduction on shell scripting, see the BASH Programming HOW-TO at the Linux Documentation Project ( tldp.org ).

Running startup scripts | Compute Engine Documentation, If an instance level startup script is specified, any project level script doesn't run. Before you begin. If you want to use the command-line examples in this guide:. The script is trying to run on the computer when it starts up. If you want the users to pick up the drive when they log on, you'll need to put it in User Configuration | Windows Settings | Scripts

The startup script doesn't run · Issue #7 · ROSbots , Hi there, Recently, I used the rosbots image on my raspberry pi (The headless version). The start up script for the ros (in the /etc/init.d folder)  Now you need to copy the file with your PowerShell script to the domain controller. Click the Show Files button and drag the file with the PowerShell script (ps1 extension) into the opened File Explorer window (the console will automatically open the folder \\yourdomainname\SysVol\yourdomainname\Policies\{Your_GPO_GUID }\Machine\Scripts\Startup of your policy in the SysVol on the nearest AD

init.d script not running on startup, Just having the script present in /etc/init.d is not sufficient to have it run at startup. To add it to your startup, you have to tell Ubuntu about it: sudo update-rc.d 

Comments
  • I have the same issue. I think the problem is with "screen"
  • I have the same issue, too. When I run the script above sudo google_metadata_script_runner --script-type startup, it works perfectly, but if I reset my VM instance the startup script doesn't work.
  • Finally, I got the answer like below
  • I have already added the startup script wen creating the instance - As I said the script works when i run it manually with sudo google_metadata_script_runner --script-type startup but not when the VM restarts. prntscr.com/f9zvhg
  • ok, great! did you check the logs in the instance? are there any errors?
  • Does not seem to be any errors from what I can tell. - pastebin.com/n3pTegUn
  • As per the screen shot the script is working without errors. Can you edit the question with the exact issue?
  • Edited the question although I'm not sure how else I can explain it since there are no errors.