- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
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.
Lab Info
Table of Contents
-
Challenge
Verify That the crond Service Is Enabled and Running
Ensure that
crond.serviceis active and enabled:systemctl status crond.serviceIf we see an active (running) status, then everything is good to go.
-
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.shMake it executable:
chmod a+x /usr/local/bin/loadavg.shRun the script:
/usr/local/bin/loadavg.shCheck to see if the script sent data to
/var/log/loadavg.log:cat /var/log/loadavg.logWe should see a timestamp and the three load averages in there.
-
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 -eto create the following content:# Min Hour DoM Month DoW Command * 8-17 * * 1-5 /usr/local/bin/loadavg.shSave the cronjob and check our work:
crontab -l -
Challenge
Verify Cronjob is Running and Producing Correct Output
Again, we could verify
crond.serviceis running usingsystemctl. If we want to know whether our job is running or not, we can runtail /var/log/cronafter a few minutes, and we should see entries in there for ourloadavg.shscript. 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.logIf the job is running, then we'll see contents like we did when we ran the script manually, once a minute.
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.