Skip to content Skip to sidebar Skip to footer

Running Python Script At Regular Intervals Using Cron In Virtual Machine (Google Cloud Platform)

Can anyone provide me with the steps of how to execute a Python script at regular intervals in Cron inside Virtual Machine (using Google Cloud Platform)? I read this link https://c

Solution 1:

If you are using a virtual machine as you suggest, then those instructions you've linked may not be relevant as they are for App Engine.

With a Compute Engine VM you should use the inbuilt Linux cron functionality. For these instructions, I'm going to assume that you want to execute the script every 10 minutes. You can adapt this value for your needs.

Here is how you should proceed if you want to execute a script via a cron job on GCP virtual machine.

1) Run this command to enter the crontab configuration page.

crontab -e

**Note, the above command will allow you to edit the crontab configuration for the user you are logged in as. If you would like to execute the script as the root user, add 'sudo' to the start of the command to edit the crontab configuration for the root user.

2) In the cron configuration, you will be able to add an entry for intervals in minutes, hours, days of month, month and day of the week. On the same line, you can add the command you would like to execute- in your case a command to execute your python script.

As an example, if you wanted to run the script every 10 minutes with python, you would add an entry such as this:

*/10 * * * *   /usr/bin/python /path/to/you/python/script.py

3) Once you've saved the crontab configuration and exited from the file, you need to restart the cron service for your changes to take affect. You can do this by running the following command.

sudo systemctl restart cron

There is some useful information here if you would like to discover more about running cron jobs in Linux.


Post a Comment for "Running Python Script At Regular Intervals Using Cron In Virtual Machine (Google Cloud Platform)"