Cronjobs are a simple and reliable way to periodically execute scripts on a virtual machine. However, to avoid having your virtual instance running all the time - and being charged for it - we can configure Google Cloud to start the GCE instance a few minute before the cronjob are executed, and to power it off afterwards.
We will use the following architecture:
Instructions for this architecture come from the GCP support website.
Configure cron jobs on the GCE instance
- Edit the crontab file:
- Schedule your script to run every day at a specific time (05:35 GMT in this example). Be careful with possible timezone differences between the VM and the Google schedule job.
- Verify that the cronjob has been saved with:
crontab -l
crontab -e
35 05 * * * python3 /home/sweethome/my_script.py
See unix-cron format for more info on scheduling cron tasks.
Add label to the Google Compute Engine instance
In the Google Cloud Console, go to Compute Engines > VM Instances. Edit the VM instance that will be used for the scheduled reporting, and add the following label (for example): - key: update
- value: daily
Set up the Cloud Functions
In the Console, go to Cloud Functions.
Create the start function
- Create a new function and name it
startInstancePubSub
- For Trigger, select
Cloud Pub/Sub
- For Topic, create a new topic and call it
start-vm
- For Runtime, select
Node.js 8
- On the
index.js
tab, copy and paste this code - On the
package.json
tab, copy and paste this code - For Function to execute, enter
startInstancePubSub
. - Click Create
Create the stop function:
- Create a new function and name it
stopInstancePubSub
- For Trigger, select
Cloud Pub/Sub
- For Topic, create a new topic and call it
stop-vm
- For Runtime, select
Node.js 8
- On the
index.js
tab, copy and paste this code - On the
package.json
tab, copy and paste this code - For Function to execute, enter
stopInstancePubSub
. - Click Create
Verify that the functions work with these instructions
Set up the Cloud Scheduler jobs
In the Console, go to Cloud Scheduler.
Create the start job
- Create a new Job and name it
start-rmn-vm
- For Frequency, enter a time ~5 minutes before the cronjob time on the GCE instance For example, if the cronjob is defined at 06:35, enter
30 6 * * *
- For Target select
Pub/Sub
, and for Topic enterstart-vm
- For Payload, enter the following:
{"zone":"europe-west1-d","label":"update=daily"}
- Click Create
Create the stop job
- Create a new Job and name it
stop-rmn-vm
- For Frequency, enter a time sufficiently after the cronjob time on the GCE instance. For example, if the cronjob is defined at 06:35, enter
0 7 * * *
to schedule the shutdown 25 minutes afterwards. - For Target select
Pub/Sub
, and for Topic enterstop-vm
- For Payload, enter the following:
{"zone":"europe-west1-d","label":"update=daily"}
- Click Create
Verify that the events work correctly with these instructions