Google Cloud SDK代码可通过cron执行 [英] Google cloud SDK code to execute via cron

查看:83
本文介绍了Google Cloud SDK代码可通过cron执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement an automated code to shut down and start VM Instances in my Google Cloud account via Crontab. The OS is Ubuntu 12 lts and is installed with Google service account so it can handle read/write on my Google cloud account.

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

My actual code is in this file /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"

当我这样调用上面的文件时,

When I call the above file like this,

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

它完美地工作了.

现在,我想将其设置为cron,以便每小时自动执行一次.所以我做到了

Now I wanted to set this up in cron so it would do automatically every hour. So I did

$ sudo crontab -e

并在cron中添加了此代码

And added this code in cron

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

并使脚本可执行

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

我还通过添加带有示例消息的创建.txt文件的示例命令来测试了crontab,它工作得很好.

I also tested the crontab by adding a sample command of creating .txt file with a sample message and it worked perfect.

但是,用于gcloud SDK的上述代码无法通过cron工作.VM不会停止,也不会在我的GC计算引擎中启动.

But the above code for gcloud SDK doesn't work through cron. The VM doesn't stop neither starts in my GC compute engine.

任何人都可以帮忙吗?

非常感谢您.

推荐答案

您已将条目添加到root的crontab中,同时为其他用户设置了Cloud SDK安装(我猜是ubu121lts).

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

您应使用以下命令将条目添加到ubu121lts的crontab中:

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

crontab -u ubu121lts -e

此外,您的条目当前计划在每小时的第0分钟运行.那是你想要的吗?

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

这篇关于Google Cloud SDK代码可通过cron执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆