Search code examples
bashgoogle-cloud-platformgoogle-compute-enginestartupscript

Save a file in a startup-script on Google Cloud Platform when launching a compute engine


I have a startup-script that works well when I launch a compute engine on google cloud. Nevertheless it doesn't seem to execute the following command. echo ${path} > ~/pd.txt Actually I can't retrieve my file when I look for it at the indicated path. Do you have any clue on how I could save a file during startup-script ?


Solution

  • When startup scripts run on GCE, they run under the root account.

    The value of ~ depends on the user. Therefore for startup scripts ~ is /root. If you login with the user name bob.jones then ~ is /home/bob.jones.

    You will find the results of echo ${path} > ~/pd.txt located at /root/pd.txt

    A couple of tips with start scripts:

    1. Do not expect $PATH to exist. Always specify the full path for both programs and filenames.
    2. Do not use environment variables.
    3. Do not use ~. In your example specify the output filename full path.