I feel like I have put enough time in on this and am asking the community, my apologies if the doc/article is right in front of me. I may not be googling correctly.
Startup scripts, specifically, the differences between AWS and GCP is causing headaches. I have a startup script that I use for launching/building AWS VMs. When I try to launch an Ubuntu 22.04 server on GCP, the startup script is executing and doing *most* of what is expected. A couple of lines are not, and I cannot find the actual script that is being used and/or the output from this script.
My script creates a user, sets permissions, etc... that all works. But, I also have a bash function to write updates to a /tmp/startup.script.out file. When the script is complete, the work is done but the log is not present.
I'm using this simple function to write to a file. Where is my output in GCP?
# this function simply writes a log message with a timestamp
function _memo { MSG=$1; echo "[$(date +'%m-%d-%y %T')] $MSG" >> $LOG_FILE; }
I have checked in the journalctl:
sudo journalctl -u google-startup-scripts.service
I also did not find it under several paths to look under (for many distributions).
I can see the entries (in journalctl/syslog) that create my user and provide sudo access. I have other commands that write to an .ssh/authorized_keys file without issue. WHERE is my log output? I have tried writing to /tmp, /root, /etc, /var... nothing seems to stick.
I know that GCP handles 'startup scripts' differently than AWS, by running the script each reboot (as opposed to once: userdata). Cool, I can work with that, but I want to put a lock file, that says, the script has already run, no need to replay these steps.
I use this (completed) file to trigger Ansible runs, "if 'startup.script.complete' exists, run ansible playbook" ... without the file, the ansible never kicks off. I'm sure there are work arounds but simply as a curious IT guy, where the heck is my stdout? Where is my logfile?
Anyone have a quick answer? Or, just point me to the right article... I'm missing something.
Thanks in advance,
Silvus
Solved! Go to Solution.
Hi,
Thank you for contacting us.
Your concern is correct, GCP runs the script on each reboot. Regarding your questions about where your stdout and your logfile are, as you mention, you can check that with the following command:
sudo journalctl -u google-startup-scripts.service
However, you can check the view output through serial port 1 and check for GCEMetadataScripts events.
For more context:
The metadata server is particularly useful when used in combination with startup and shutdown scripts because you can use the metadata server to programmatically get unique information about a VM, without additional authorization.
For example, you can write a startup script that gets the metadata key:value pair for a VM's external IP and use that IP in your script to set up a database. Because the default metadata keys are the same on every VM, you can reuse your script without having to update it for each VM. This helps you create less brittle code for your applications. You can learn more about this here.
Regards!!