Google云SDK代码通过cron执行

时间:2015-06-13 21:15:08

标签: cron google-cloud-platform gcloud

我正在尝试实施自动代码,以通过Crontab关闭并启动我的Google Cloud帐户中的VM实例。该操作系统是Ubuntu 12 lts,并安装了Google服务帐户,因此它可以处理我的Google云帐户的读/写。

我的实际代码在此文件/home/ubu12lts/cronfiles/resetvm.sh

#!/bin/bash
echo Y | gcloud compute instances stop my-vm-name --zone us-central1-a
sleep 120s
gcloud compute instances start my-vm-name --zone us-central1-a
echo "completed"

当我这样调用上述文件时,

$ bash /home/ubu12lts/cronfiles/resetvm.sh

它完美无缺并且能够胜任。

现在我想在cron中设置它,这样它每小时都会自动完成。所以我做了

$ sudo crontab -e

并在cron中添加了此代码

0 * * * *  /bin/sh /home/ubu12lts/cronfiles/resetvm.sh >>/home/ubu12lts/cron.log

并使脚本可执行

chmod +x /home/ubu12lts/cronfiles/resetvm.sh

我还通过添加一个创建带有示例消息的.txt文件的示例命令来测试crontab,它完美无缺。

但是上面的gcloud SDK代码并没有通过cron工作。 VM不会在我的GC计算引擎中停止启动。

有人可以帮忙吗?

非常感谢你。

2 个答案:

答案 0 :(得分:2)

You have added the entry to root's crontab, while your Cloud SDK installation is setup for a different user (I am guessing ubu121lts).

You should add the entry in ubu121lts's crontab using:

crontab -u ubu121lts -e

Additionally your entry is currently scheduled to run on the 0th minute every hour. Is that what you intended?

答案 1 :(得分:0)

我之前也遇到过类似的问题。我通过在我的 script.sh 中强制配置文件来修复它,用它加载 gcloud 环境变量。下面的例子:

<块引用>

#!/bin/bash

源/etc/profile

回声 Y | gcloud 计算实例停止 my-vm-name --zone us-central1-a 睡眠 120 秒 gcloud 计算实例启动 my-vm-name --zone us-central1-a 回声“已完成”

这也帮助我调整了 GKE 中的节点数。