我正在使用 gcloud 命令來部署簡單的虛擬機。我正在使用啟動腳本來配置機器上所有必需的包。我們的包裹取自nexus。有時我們的更改或連接網路問題會導致啟動腳本失敗。我配置了一段代碼來驗證啟動腳本回傳代碼。我運行 gcloud create 命令并提供啟動腳本,驗證啟動腳本輸出的代碼塊如下所示。
echo "Step 2: sleep till startup script finishes"
## block to check return code of the VM startup script
MAX_WAIT_TIME=1200
WAIT_TIME=0
RC_CODE=""
echo "[INFO] waiting for startup-script return code"
while [ -z $RC_CODE ] && [ $WAIT_TIME -le $MAX_WAIT_TIME ]
do
RC_CODE=$(gcloud compute instances get-serial-port-output gce-$GCP_ZONE-d-$APP-dev \
--project=$GCP_PROJECT_ID \
--zone=$GCP_ZONE | awk '/^[0-9]{4}/ && /google_metadata_script_runner/ && /startup-script-url exit status/ {sub("\r", "", $NF); print $NF}')
if [[ -z $RC_CODE ]] ; then
echo -n "."
WAIT_TIME=$((WAIT_TIME 10))
sleep 10
else
if [[ $RC_CODE -eq 0 ]] ; then
echo -e "\n[INFO] startup script completed with return code $RC_CODE."
break
else
echo -e "\n[INFO] Startup script completed with return code $RC_CODE."
exit $RC_CODE
fi
fi
done
# to check timeout scenario
if [[ -z $RC_CODE ]]
then
echo "[INFO] Startup script timed out after $((MAX_WAIT_TIME/60))."
echo "[INFO] Startup script completed with return code 1."
exit 1
fi
我的輸出看起來像這樣,
./sh/create-dev-vm-app.sh
Deleted [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
Created [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
waiting for startup-script return code
Specify --start=2473 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=32157 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=37602 in the next get-serial-port-output invocation to get only the new output starting from here.```
.
Startup script completed with return code 0.
如何抑制這些“指定”行出現在 o/p 螢屏上?和/或在我收到回傳碼后列印從 get-serial-port-output 輸出接收到的所有啟動腳本訊息。
uj5u.com熱心網友回復:
吞下標準錯誤以避免多余的輸出:
gcloud ... 2>/dev/null | awk ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407358.html
標籤:
