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:
Now, there is a specific format to enter the details in the crontab editor. Here is an example:
- - - - -
| | | | |
| | | | +----- 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:
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:
chmod +x croninfo.sh
Now, you can enter the commands to check the CPU usage, disk usage, and memory usage:
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:
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.