0

I have a bash script for a cron job that uses Google Cloud SDK's gcloud command after some conditional statements.

eg.

gcloud compute firewall-rules update allow-ssh --source-ranges=$(echo "${mylocations[@]}" | tr ' ' ',')

It is run using crontab like this:

*/2 * * * *        /home/user/bin/firewall-update.sh >/dev/null

I have an email MTA set up, so I can get emails regarding any errors from the server, but stdout is redirected to /dev/null.

The problem is that gcloud output goes to stderr, even when run successfully, so I always get an email with these sort of line whenever the script is run.

TITLE: Cron user@host /home/user/bin/firewall-update.sh >/dev/null

Updated [https://www.googleapis.com/compute/v1/projects/project-name-4234534/global/firewalls/allow-ssh].

Updated [https://www.googleapis.com/compute/v1/projects/project-name-4234534/global/firewalls/allow-mosh].

...

I have tried adding --verbosity=error, critical and noneto the gcloud commands, but they have no effect.

Why does the output go to stderr when it completes successfully and how can I stop getting these emails, while still getting them when there is an error?

1
  • 1
    Capture stderr and only print it if the exit status isn't 0?
    – muru
    Commented Aug 13, 2020 at 4:12

1 Answer 1

0

The solution is to add this global flag, which applies to all gcloud commands:

--no-user-output-enabled

    Print user intended output to the console. 
    Overrides the default core/user_output_enabled property value for this command invocation. 
    Use --no-user-output-enabled to disable.

https://cloud.google.com/sdk/gcloud/reference#--user-output-enabled

Actual errors are still directed to stderr.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .