Schedule shutdown and restart

2,194 views
Skip to first unread message

dhilip kumar

unread,
Jun 7, 2015, 10:01:27 PM6/7/15
to gce-dis...@googlegroups.com
Dear all,

I would like to run my server only in scheduled hours a day.like, it should shutdown at midnight 12 and walk up at 7. How can I achieve that?
OS : debian 7
Server type : webserver
Additional info: I have installed vesta panel where I cron job.

Faizan (Google Cloud Support)

unread,
Jun 7, 2015, 11:27:35 PM6/7/15
to gce-dis...@googlegroups.com, dhilip kumar
Hello Dhilip,

You use command line (gcloud tool) or Google Compute API to start or stop the instances. You can implement any of that method in your script.

Moreover, you can take a look at Preemptible instances which are recently announced. These instances runs on a periodic basis and are very suitable for jobs like batch processing.

Faizan

Ken Moore

unread,
Jun 7, 2015, 11:38:58 PM6/7/15
to gce-dis...@googlegroups.com, dhilip kumar
Hi Dhilip,

I recently addressed the same problem in this way: I created a f1-micro VM running some node.js code tasked solely with turning up and down my other more expensive VM on a daily schedule.  Check the documentation for the google-oauth-serviceaccount npm module, you'll need to include an oauth-config.json file and your googleapi-privatekey.pem file alongside your module. I've pasted the code below, hopefully it will be useful to you.

Ken

// Controller
// Starts the cloudserver VM at 7am, stops it at 7pm

var request = require('request');
const gaccount = require('google-oauth-serviceaccount');
var cron = require('cron');

var CronJob = cron.CronJob;

new CronJob('00 00 07 * * *', function() {
  updateCloudServer('start');
}, null, true, "America/Los_Angeles");

new CronJob('00 00 19 * * *', function() {
  updateCloudServer('stop');
}, null, true, "America/Los_Angeles");

<index.js below...>

function updateCloudServer(type) {
  gaccount.auth(function(err, access_token) {
    var token = "?access_token=" + access_token;

    var options = {
                  "zones/us-central1-b/instances/cloudserver/" + type + token,
          json: true
    }

    request.post(options, function(err, res, body) {
      if (err) {
        console.log('An error occurred posting the ' + type + ' request.');
        console.log(err);
      }
    });
  });
}

<EOF>
-------------------------------------------------
<oauth-config.json below (that's not my actual service account email, but yours should look much like that)...>
{
  "keyFile": "googleapi-privatekey.pem",
  "expiresInMinutes": 60,
  "claim": {
  }
}

<EOF>

Ken Moore

unread,
Jun 7, 2015, 11:42:52 PM6/7/15
to gce-dis...@googlegroups.com
Oops, I had a copy-paste error -- the line that says "<index.js below...>" appears in the middle of the index.js block.

To clarify, the code is:


<index.js below...>

// Controller
// Starts the cloudserver VM at 7am, stops it at 7pm

var request = require('request');
const gaccount = require('google-oauth-serviceaccount');
var cron = require('cron');

var CronJob = cron.CronJob;

new CronJob('00 00 07 * * *', function() {
  updateCloudServer('start');
}, null, true, "America/Los_Angeles");

new CronJob('00 00 19 * * *', function() {
  updateCloudServer('stop');
}, null, true, "America/Los_Angeles");

Steven Edouard

unread,
Mar 20, 2017, 8:22:03 PM3/20/17
to gce-discussion
There's also a 3rd party service out there called VMPower which lets you schedule VMs to power on/off/resize with a simple calendar. It's easier and more flexible than writing a script.

Sirui Sun

unread,
Nov 14, 2018, 2:51:04 PM11/14/18
to gce-discussion
Piling on: we've also recently released a step-by-step tutorial for scheduling start/stop of your VMs using Google Cloud Scheduler and Google Cloud Functions. It should get you going with this in minutes, and also give you the foundation to further customization. Please take a look!

And, we're also looking into ways to make setting up schedules even easier. 

As always feedback and questions are welcome.

Thanks,
-Sirui
Product Manager | Google Compute Engine

Fernando de Diego

unread,
Jul 16, 2020, 11:14:41 PM7/16/20
to gce-discussion
Hi Sirui,
I know this thread is pretty old, but I was looking for that tutorial and I can fin dit anymore. Do you know where I can found it?

Thanks!
Fernando

Digil (Google Cloud Platform Support)

unread,
Jul 17, 2020, 11:08:58 AM7/17/20
to gce-discussion
Hello Fernando,

You need to create a Cloud Function (there is this third party article where you can refer some examples for designing a cloud function in-order to start and stop an instance). Once the  Cloud Function gets created, then you need to configure the Cloud Scheduler to trigger the HTTP address of each Cloud function. For creating and configuring cron jobs using Cloud Scheduler, you can refer this help center article.

You may also make use of cron(linux based) to achieve the same. You can find more details on how to set-up a cron for a nightly reboot in the stackexchange.com discussion thread. 

Sirui Sun

unread,
Jul 17, 2020, 12:01:21 PM7/17/20
to Fernando de Diego, gce-discussion
Hi Fernando - that is indeed strange. I am looking into where the doc went.

-Sirui

--
© 2018 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
 
Email preferences: You received this email because you signed up for the Google Compute Engine Discussion Google Group (gce-dis...@googlegroups.com) to participate in discussions with other members of the Google Compute Engine community and the Google Compute Engine Team.
---
You received this message because you are subscribed to a topic in the Google Groups "gce-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gce-discussion/f3hIAOO3E1M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gce-discussio...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/3c16da11-249d-43e5-9628-0f7456627092n%40googlegroups.com.

Fernando de Diego

unread,
Jul 17, 2020, 1:33:08 PM7/17/20
to gce-discussion
Thank you Digil,
I was looking for that step-by-step tutorial because I've found many references to it on the web, but not the tutorial itself.

Regarding the article that you mention, do you think those google functions will work for a Flexible instance?


Reply all
Reply to author
Forward
0 new messages