gcloud 命令在 crontab -e 中不起作用?

gcloud commands not working in crontab -e?

我的 google 云项目中几乎没有项目 运行,我正试图在特定时间停止它们并在特定时间开始。

我创建了一个用于停止的脚本文件test.sh

#!/bin/bash
gcloud compute instances stop instance1

现在我尝试 crontab -e 并添加了这个,

30 01 1-31 1-12 1-5 /home/user/test.sh >> /var/log/test.log

但这并没有停止我的 gcp 实例,也没有创建任何日志文件。

更新,

我只是对时间安排感到困惑

00 10 * * 1-5 start.sh
00 01 * * 1-5 stop.sh

00 01 * * 6 stop.sh

这在周六和周日如何运作?我想在星期六 1am 停止实例并在星期一 10am 重新启动它。我的配置正确吗?

从周一到周五,它应该在凌晨 1 点关闭并在早上 10 点开始。

`crontab` 运行s 作为 root (!?),因此未针对 `gcloud` 进行身份验证

可能有更好的选择,但作为解决方案的概述:

  • 创建一个具有适当权限的服务帐户,供 cron
  • 使用
  • 在实例上为 root 激活服务帐户 gcloud auth activate-service-account(在启动期间或每个 cron 任务)
  • 运行 cron 个职位

201126 更新

抱歉,我误读了你的问题并认为你也想 运行 cron 在 Compute Engine 实例上。

我想你想 运行 cron 本地。

诀窍 (!) 是确保您的命令使用绝对路径。

which gcloud

对我来说,这个 returns /snap/bin/gcloud 因为我正在 运行 宁 Ubuntu 并快速安装了 gcloud。这对您来说可能会有所不同。

那么,以下对我有用:

stop.sh:

#!/usr/bin/env bash

PROJECT=[[YOUR-PROJECT]]
INSTANCE=[[YOUR-INSTANCE]]
ZONE=[[YOUR-ZONE]]

echo "[$(date --rfc-3339=seconds)] Stopping ${INSTANCE}"

/snap/bin/gcloud compute instances stop ${INSTANCE} \
--project=${PROJECT} \
--zone=${ZONE} \
--verbosity=debug

echo "[$(date --rfc-3339=seconds)] Stopped ${INSTANCE}"

NOTE I've added --verbosity=debug but you may prefer to reduce or omit this entirely.

确保脚本可执行:

chmod +x /path/to/stop.sh

然后crontab -e:

# Whosebug: 64994379
* * * * * /path/to/stop.sh >>/path/to/stop.log 2>&1

NOTE You want 30 01 1-31 1-12 1-5 here. This may be simpler as 30 01 * * 1-5

并且:

[2020-11-26 09:39:01-08:00] Stopping [[YOUR-INSTANCE]]
DEBUG: Running [gcloud.compute.instances.stop] with arguments: [--project: "[[YOUR-PROJECT]]", --verbosity: "debug", --zone: "[[YOUR-ZONE]]", INSTANCE_NAMES:1: "['[[YOUR-INSTANCE]]']"]
Stopping instance(s) [[YOUR-INSTANCE]]...
......done.
Updated [https://compute.googleapis.com/compute/v1/projects/[[YOUR-PROJECT]]/zones/[[YOUR-ZONE]]/instances/[[YOUR-INSTANCE]]].
INFO: Display format: "none"
DEBUG: SDK update checks are disabled.
[2020-11-26 09:39:04-08:00] Stopped [[YOUR-INSTANCE]]
[2020-11-26 09:40:01-08:00] Stopping [[YOUR-INSTANCE]]
DEBUG: Running [gcloud.compute.instances.stop] with arguments: [--project: "[[YOUR-PROJECT]]", --verbosity: "debug", --zone: "[[YOUR-ZONE]]", INSTANCE_NAMES:1: "['[[YOUR-INSTANCE]]']"]
Stopping instance(s) [[YOUR-INSTANCE]]...
.....done.
Updated [https://compute.googleapis.com/compute/v1/projects/[[YOUR-PROJECT]]/zones/[[YOUR-ZONE]]/instances/[[YOUR-INSTANCE]]].
INFO: Display format: "none"
DEBUG: SDK update checks are disabled.
[2020-11-26 09:40:04-08:00] Stopped [[YOUR-INSTANCE]]
[2020-11-26 09:41:01-08:00] Stopping [[YOUR-INSTANCE]]
DEBUG: Running [gcloud.compute.instances.stop] with arguments: [--project: "[[YOUR-PROJECT]]", --verbosity: "debug", --zone: "[[YOUR-ZONE]]", INSTANCE_NAMES:1: "['[[YOUR-INSTANCE]]']"]
Stopping instance(s) [[YOUR-INSTANCE]]...
......done.
Updated [https://compute.googleapis.com/compute/v1/projects/[[YOUR-PROJECT]]/zones/[[YOUR-ZONE]]/instances/[[YOUR-INSTANCE]]].
INFO: Display format: "none"
DEBUG: SDK update checks are disabled.
[2020-11-26 09:41:05-08:00] Stopped [[YOUR-INSTANCE]]

Google Cloud Scheduler 为您提供 运行ning 的额外好处,无论您的主机 (运行ning cron) 是否 运行宁.

如果您想非常勤奋,您可以扩充脚本以遍历所有项目并关闭每个实例。在这种情况下,最好让脚本也通知您它的成功或失败,以免您沾沾自喜。

我建议遵循有关

的官方文档

Scheduling compute instances with Cloud Scheduler


1. Compute Engine Instance: A Compute Engine instance we want to run on a
schedule.

2.Cloud Functions functions: Functions to start and stop the instance we
want to schedule.

3.Pub/Sub messages: Messages sent and received for each start and stop
event.

4.Cloud Scheduler jobs: Jobs to make calls on a set schedule to start
and stop the instance.