1

I am trying execute some gcloud commands in bash script from crontab. The script execute sucessfully from command shell but not from the cron job.

I have tried with:

  1. Settng the full path to gcloud like:

     /etc/bash_completion.d/gcloud
     /home/Arturo/.config/gcloud
     /usr/bin/gcloud
     /usr/lib/google-cloud-sdk/bin/gcloud
    
  2. Setting in the begin the script:

      /bin/bash -l
    
  3. Setting in the crontab:

    51 21 30 5 6 CLOUDSDK_PYTHON=/usr/bin/python2.7; 
    /home/myuser/folder1/myscript.sh param1 param2 param3 -f >> 
    /home/myuser/folder1/mylog.txt`
    
  4. Setting inside the script:

    export CLOUDSDK_PYTHON=/usr/bin/python2.7

  5. Setting inside the script:

    sudo ln -s /home/myuser/google-cloud-sdk/bin/gcloud /usr/bin/gcloud

Version Ubuntu 18.04.3 LTS command to execute: gcloud config set project myproject

but nothing is working, maybe I am doing something wrongly. I hope you can help me.

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • is gcloud in your path for the user who's cron you've set? – Gabe Weiss Jun 01 '20 at 04:52
  • Sorry, I missed the sudo ln... which SHOULD work (should). BUT, keeping in mind, you won't be authenticated...the cron environment won't be authenticated with `gcloud init` so you'll need to start off running something that will authenticate gcloud, probably with a service account would be the best way. – Gabe Weiss Jun 01 '20 at 06:23
  • The first command that I executed is: gcloud auth activate-service-account --key-file=fileauth.json. How can i check if gcloud is in the path for the user in the cron? Need I too run "gcloud init" before? – Arturo Díaz Jun 01 '20 at 14:18
  • gcloud init doesn't need to be run if you're doing the gcloud auth activate-service-account, that's enough. But you DO still need to set the project. Might that be it? Need to run `gcloud config set project my_project` after the activate-service-account call. – Gabe Weiss Jun 01 '20 at 17:03
  • I found my error, the problem here was only in the command: "gcloud dns record-sets transaction start", the others command was executing sucesfully but only no logging nothing, by that I though that was not executng the other commands. This Command create a temp file ex. transaction.yaml and that file could not be created in the default path for gcloud(snap/bin), but the log simply dont write any thing!. I had to specify the path and name for that file with the flag --transaction-file=mytransaction.yaml. Thanks for your supprot and ideas – Arturo Díaz Jun 02 '20 at 02:36
  • Oh good! There's no way in a million years I'd have found that. :D Glad you got it working! – Gabe Weiss Jun 02 '20 at 03:47

4 Answers4

1

You need to set your user in your crontab, for it to run the gcloud command. As well explained in this other post here, you need to modify your crontab to fetch the data in your Cloud SDK, for the execution to occur properly - it doesn't seem that you have made this configuration.

Another option that I would recommend you to try out, it's using a Cloud Scheduler to run your gcloud commands. This way, you can use gcloud for your cron jobs in a more integrated and easy way. You can verify more information about this option here: Creating and configuring cron jobs

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • Thanls gso_gabriel I read thats article but is the same result not working "crontab -u myuser -e" I read about cron scheduler but seem me that I need to knowk some language for do it , this is a bash script only. I beleie only accept functions, pub/sub and app engine – Arturo Díaz Jun 01 '20 at 14:30
  • I found my error, the problem here was only in the command: "gcloud dns record-sets transaction start", the others command was executing sucesfully but only no logging nothing, by that I though that was not executng the other commands. This Command create a temp file ex. transaction.yaml and that file could not be created in the default path for gcloud(snap/bin), but the log simply dont write any thing!. I had to specify the path and name for that file with the flag --transaction-file=mytransaction.yaml. Thanks for your supprot and ideas – Arturo Díaz Jun 02 '20 at 02:36
  • Nice to hear it helped you! Please, consider upvoting my answer, if you think it helped you. :) – gso_gabriel Jun 02 '20 at 06:38
1

I found my error, the problem here was only in the command: "gcloud dns record-sets transaction start", the others command was executing sucesfully but only no logging nothing, by that I though that was not executng the other commands. This Command create a temp file ex. transaction.yaml and that file could not be created in the default path for gcloud(snap/bin), but the log simply dont write any thing!. I had to specify the path and name for that file with the flag --transaction-file=mytransaction.yaml. Thanks for your supprot and ideas

0

I have run into the same issue before. I fixed it by forcing the profile to load in my script.sh,loading the gcloud environment variables with it. Example below:

#!/bin/bash
source /etc/profile
gcloud config set project myprojectecho 
echo "Project set to myprojectecho."

I hope this can help others in the future with similar issues, as this also helped me when trying to set GKE nodes from 0-4 on a schedule.

David
  • 91
  • 1
  • 4
0

Adding the below line to the shell script fixed my issue

#Execute user profile

source /root/.bash_profile