Skip to main content

Command Palette

Search for a command to run...

Schedule a Task on VM (Using Cronjob)

Set up a cron job to automate recurring tasks on your VM.

Published
3 min read
  1. Login to your google cloud account and create a VM.

    VM is ready.

  2. SSH into Your VM Instance

    • Select Authorize if prompted.

To set up a cron job, you need to access your VM instance.

  • Open Google Cloud Console and go to the Compute EngineVM instances.

  • Locate your VM instance and click on its name to open the details page.

  • Click on the SSH button to connect to your VM.

  1. Write a Script to Automate Tasks (Example: Start/Stop VM)

    You can create a shell script to manage the VM instances. For example, let’s automate the process of stopping and starting a VM.

    • Create a shell script (e.g., my-vm-instance.sh):

        nano my-vm-instance.sh
      

      Press Enter

    • Add the task to the script. In this example, the script will stop an instance:

        #!/bin/bash
        gcloud compute instances stop [INSTANCE_NAME] --zone=[ZONE]
      
    • If you want the script to start the instance instead, change it to:

        #!/bin/bash
        gcloud compute instances start [INSTANCE_NAME] --zone=[ZONE]
      

      Save and exit nano by pressing CTRL + X, then press Y and hit Enter.

    • Make the script executable:

        chmod +x my-vm-instance.sh
      

  2. Schedule the Script Using Cron

    • Run the following command to open the crontab file:

        crontab -e
      
    • Choose from [1-3] for editor, I’m choosing nano editor for that pressing 1.

    • Nano editor will get open.

    • Add a Cron Job.

    • For example, to stop the instance every day at 10 PM (22:00):

      In the crontab editor, you can add your cron job. The syntax for a cron job is:

    * * * * * /path/to/command

Where the five asterisks represent:

  • Minute (0-59)

  • Hour (0-23)

  • Day of the month (1-31)

  • Month (1-12)

  • Day of the week (0-7) (Sunday is both 0 and 7)

      0 22 * * * /path/to/my-vm-instance.sh
    

    Or to start the instance every day at 5 AM:

    (I’m showing this example to you)

      0 5 * * * /path/to/my-vm-instance.sh
    

    Replace /path/to/my-vm-instance.sh with the actual path to your script.

  • Save and Exit

    After adding your cron job, save the changes and exit the editor (in nano, press CTRL + X, then Y, and Enter).

  1. Verify Cron Job

    You can list your current cron jobs to verify that your job has been added successfully.

    • Run the following command:

        crontab -l
      

  2. Check Cron Job Execution

    To ensure your cron job runs as expected:

    • Check the log files (usually located in /var/log/syslog or /var/log/cron.log, depending on your OS) for any errors or confirmation of execution:

        sudo grep CRON /var/log/syslog
      

    • If your task generates output, consider logging it in your script. For example:

        ~/my-vm-instance.sh >> ~/logfile.log 2>&1
      
    • Check the Log File Again

      After running the script, check the log file to see if the echo statements and command outputs were logged:

        cat ~/logfile.log
      

      That's how logs are generated, allowing us to track and verify the execution of the script by reviewing the output and any errors captured in the log file.

Conclusion

By setting up a cron job with a script that uses gcloud commands, you can automate the management of your Google Cloud Compute Engine instances. This approach allows you to perform tasks like starting, stopping, or managing your instances on a recurring schedule without manual intervention.

GCP VM Series: From Zero to Compute

Part 14 of 14

Get ready for an exciting ride in Google Cloud! Our series takes you through every method of creating virtual machines in GCP, complete with engaging tutorials and practical labs. Elevate your cloud game and become a VM virtuoso!

Start from the beginning

Create a Simple VM (Compute Engine Instance)

Launch a basic VM using default settings.