Creating a Cronjob to Run a Script Periodically

Get Started
15 minutes
  • 4 Learning Objectives

About this Hands-on Lab

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

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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.

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.

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
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.

Additional Resources

The administrators are concerned about the load on the system impacting application performance during the work week (Monday through Friday). They want us to create a script that will gather the time and system load information, and then configure cron execute it once per minute between 8 AM and 5 PM. Once we've gotten it done, we'll need to verify that the script's output is going to the the correct log file, /var/log/loadavg.log.

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user. We'll need to be root right away, so let's run sudo -i as soon as we get in, then we can continue.

What are Hands-on Labs

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?