Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Creating a Cronjob to Run a Script Periodically

Using `cron` jobs allow us to run processes according to a recurring schedule. We can set them to run at set times at regular intervals, to perform functions like backups, send emails, or most anything else we might want to do, which can be very useful for a System Administrator.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 15m
Published
Clock icon Jan 17, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Verify That the crond Service Is Enabled and Running

    Ensure that crond.service is active and enabled:

    systemctl status crond.service
    

    If we see an active (running) status, then everything is good to go.

  2. Challenge

    Verify that /usr/local/bin/loadavg.sh is Executable for All and Produces Correct Output

    Check permissions on /usr/local/bin/loadavg.sh:

    ls -l /usr/local/bin/loadavg.sh
    

    Make it executable:

    chmod a+x /usr/local/bin/loadavg.sh
    

    Run the script:

    /usr/local/bin/loadavg.sh
    

    Check to see if the script sent data to /var/log/loadavg.log:

    cat /var/log/loadavg.log
    

    We should see a timestamp and the three load averages in there.

  3. Challenge

    Create a cron Job that Executes /usr/local/bin/loadavg.sh Once per Minute During the Hours of 8AM-5PM on Monday through Friday

    Use crontab -e to create the following content:

    # Min   Hour    DoM     Month     DoW     Command
       *    8-17     *        *       1-5     /usr/local/bin/loadavg.sh
    

    Save the cronjob and check our work:

    crontab -l
    
  4. Challenge

    Verify Cronjob is Running and Producing Correct Output

    Again, we could verify crond.service is running using systemctl. If we want to know whether our job is running or not, we can run tail /var/log/cron after a few minutes, and we should see entries in there for our loadavg.sh script. We should also take a look at the log that the script is writing to, with:

    systemctl status crond.service
    tail /var/log/cron
    cat /var/log/loadavg.log
    

    If the job is running, then we'll see contents like we did when we ran the script manually, once a minute.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans