2

So I installed the google cloud sdk by downloading the archive as follows:

$ wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-225.0.0-linux-x86_64.tar.gz
$ tar -xvzf google-cloud-sdk-225.0.0-linux-x86_64.tar.gz

The above creates google-cloud-sdk directory with relavent files and I run install.sh which installs gcloud.

When I access gcloud from the shell, it behaves as expected. But when I try to access it via crontab, I get the error as stated in the title.

Any reason why could be the case??

P.S My intention is to use gcloud to perform firestore backups every 24hrs.

EDIT:
As Doug suggested, I referenced gcloud by its full path and was greeted with the following:

    Traceback (most recent call last):
  File "/home/ec2-user/google-cloud-sdk/lib/gcloud.py", line 95, in <module>
    main()
  File "/home/ec2-user/google-cloud-sdk/lib/gcloud.py", line 54, in main
    from googlecloudsdk.core.util import encoding
  File "/home/ec2-user/google-cloud-sdk/lib/googlecloudsdk/core/util/encoding.py", line 202
    for k, v in six.iteritems(env)}
      ^
SyntaxError: invalid syntax

I have set CLOUDSDK_PYTHON to /usr/bin/python2.7 (python 2.7.13) still the error persists. But the strange this is, this error doesn't happen when I run the command directly via the shell.

Answer: ok this is the behaviour of crontab which I wasn't aware of. It doesn't access the environment variables set. More info in this answer

Adarsh
  • 3,273
  • 3
  • 20
  • 44
  • Your system isn't adding gcloud to the PATH environment variable used to run your script. You'll have to do something to arrange for PATH to contain the location of glcoud, or simply refer to gcloud by its full path. – Doug Stevenson Nov 20 '18 at 17:17
  • Yup i have added gcloud to PATH and can access it via the shell. But not so via crontab which is quite bizzare. – Adarsh Nov 20 '18 at 18:25
  • 1
    crontab likely won't use your shell login settings. – Doug Stevenson Nov 20 '18 at 19:00
  • Yea Doug. seems to be the case. Changing the entry in crontab from `path_to_script.sh >> path_to_log_file 2>&1` to `export CLOUDSDK_PYTHON=/usr/bin/python2.7; path_to_script.sh >> path_to_log_file 2>&1` solves it – Adarsh Nov 20 '18 at 19:08

4 Answers4

2

run gsuite commands:

./google-cloud-sdk/bin/gcloud [command]
Brandon Stewart
  • 600
  • 1
  • 8
  • 12
1

Adding solution from the comments as community wiki answer

Crontab does not have access to the environment variables. By adding gcloud to the PATH environment variable used to run the scripts can resolve this issue. Also, crontab wont use shell login settings it is reccomended to change the entry of crontab from:

path_to_script.sh >> path_to_log_file 2>&1

to export:

CLOUDSDK_PYTHON=/usr/bin/python2.7; path_to_script.sh >> path_to_log_file 2>&1
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Harmit Rishi
  • 120
  • 5
0

In my case, Compute Engine with Ubuntu 20.04, I use the full path to gcloud command in front of every command as cron cannot use the environment variables as the answer suggested. The full path for Ubuntu 20 is installed via snap by default, which is at:

/snap/bin/gcloud
Fony Lew
  • 505
  • 4
  • 16
  • 1
    This is good to note, however it only applies if you are using the google-cloud-sdk snap package in Ubuntu. This would not be correct if you installed google's tarball from their website. That is `/google-cloud-sdk/bin/gcloud` that was mentioned earlier. – G_Style May 12 '23 at 15:26
0

In addition to these solutions, if you have a lot of cron jobs that run scripts with GCP commands (for example gcloud or gsutil) that do not contain absolute paths, a quick fix is to source your system environment path. Put this at the top of your script:

source /etc/profile

As long as google-cloud-sdk is setup properly on your system with Google's tarball or a packaged release of it in Ubuntu's snap, this will set the environment for google-cloud-sdk inside your script (since as we know, cron doesn't know where they live). There are other ways to do this in your scripts, like prefixing all your google commands with absolute paths or exporting the path in your script or cron itself. However, this should save you some time if suddenly your prod stuff is all broken after updating google-cloud-cli or sdk; or if you just upgraded your Ubuntu release.

G_Style
  • 588
  • 8
  • 16