如何在 cron 腳本中停止 gcloud 命令的 stderr 輸出而不停止所有錯誤輸出?

如何在 cron 腳本中停止 gcloud 命令的 stderr 輸出而不停止所有錯誤輸出?

我有一個用於 cron 作業的 bash 腳本,它gcloud在一些條件語句之後使用 Google Cloud SDK 的命令。

例如。

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

它的運作方式crontab如下:

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

我設定了電子郵件 MTA,因此我可以從伺服器獲取有關任何錯誤的電子郵件,但 stdout 被重定向到/dev/null.

問題是,即使運行成功,gcloud 輸出也會發送到 stderr,因此每當腳本運行時,我總是會收到一封包含此類行的電子郵件。

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

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

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

我嘗試將--verbosity=error,criticalnone添加到 gcloud 命令中,但沒有效果。

為什麼成功完成後輸出會轉到 stderr?

答案1

解決方案是添加此全域標誌,該標誌適用於所有 gcloud 命令:

--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

實際錯誤仍然定向到 stderr。

相關內容