我想知道实例在Google Cloud Platform上运行了多长时间

时间:2019-02-05 15:35:26

标签: google-cloud-platform google-compute-engine gcloud

我如何知道实例在Google Cloud Platform上运行(计算机)已有多长时间?

如果我运行

null

我可以知道它是否正在运行,但是我想知道它已经运行了多长时间。

3 个答案:

答案 0 :(得分:1)

如果您通过SSH进入实例,则可以运行命令uptime -p,它将显示实例已运行了多长时间。

答案 1 :(得分:1)

您可以执行以下命令:

 gcloud compute --project <project_id> ssh --zone <zone> <instance-name> -- command 'uptime'

答案 2 :(得分:0)

与正常运行时间类似,您可以使用“createdTimstamp”作为衡量标准。查看计算引擎的 REST API:cloud.google.com/compute/docs/reference/rest/v1/instances/get。在蟒蛇中:

import googleapiclient.discovery
import google.auth
credentials, project_id = google.auth.default()
compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
zone = "us-central1-a"

result = compute.instances().list(project=project_id, zone=zone).execute()
for instance in result['items']:
     print(instance['name'], instance['creationTimestamp'])

示例输出:

<块引用>

我真棒实例 2020-10-27T03:35:45.814-07:00

时间戳采用 ISO 8601 格式,可以轻松解析:How to convert a timezone aware string to datetime in Python without dateutil?