Rocky Linux

How to Configure Crontab on Rocky Linux 9

Cron Job is the utility that is used to schedule and automate the different tasks on Linux. You can use Cron Jobs to automate a particular script or commands to regulate repetitive processes.

Similarly, you can use the crontab utility to submit, edit, list, and remove the cron jobs from the system. The full form of crontab is the cron table where the word “cron” stands for the time-based job in the operating system.

Hence, if you are familiar with Unix/Linux-based OS working, it is good to learn everything about crontab. In this tutorial, we will explain the simple way to configure crontab on Rocky Linux 9 (RHEL-based OS).

How to Configure Crontab on Rocky Linux 9?

Here, we will describe the complete method and example to create, edit, use, and remove crontab. First, run the following command to open the crontab editor in the terminal:

crontab -e

Now, there is a specific format to enter the details in the crontab editor. Here is an example:

* * * * * (Commands)

- - - - -

| | | | |

| | | | +----- day of the week (0 - 6)

| | | +------- month (1 - 12)

| | +--------- day of the month (1 - 31)

| +----------- hour (0 - 23)

+------------- min (0 - 59)

As the previous table shows, five sections represent the day of the week, month, day of the month, hour, and minutes. For example, if you want to set the crontab to remove unwanted files weekly, open the terminal and add the following details:

0 0 * * 0 find /tmp -type f -mtime +7 -delete

In the previous information, we used the “find” command to find the tmp (temporary) files and “mtime” to select those files which were updated for less than a week. The line 0 0 * * 0 shows the 12 AM every Sunday night.

Let’s take another example to monitor the system through the crontab. In this case, you will require a Bash script, so run the following commands to create the Bash file and give it with executable privileges:

touch croninfo.sh

chmod +x croninfo.sh

Now, you can enter the commands to check the CPU usage, disk usage, and memory usage:

#!/bin/bash

echo "Memory usage:"

free -m

echo "Disk usage:"

df -h

echo "CPU usage:"

top -bn1 | grep "Cpu(s)" | \

sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \

awk '{print 100 - $1"%"}'

Now, open the crontab editor and add the following information:

0 10 * * * ~/croninfo.sh > ~/Documents/info.log 2>&1

The previous information means that you scheduled the crontab to run the Bash script daily at 10 AM and created an “info.log” file in the Documents directory.

Conclusion

This is all about the simple ways to configure and use crontab on Rocky Linux 9. We explained two examples to setup crontab and schedule the specific tasks easily. You can also insert and use multiple commands in a single crontab but make sure that you use it properly. Otherwise, you may face certain errors. Furthermore, you can use the crontab –help command to get a brief information about the crontab options on Rocky Linux 9.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.