How to Schedule a Cron Job in Red Hat Enterprise Linux 9

How to Schedule a Cron Job in Red Hat Enterprise Linux 9

Crontab or cron table is an excellent tool in any Linux distribution. By using the crontab, you can schedule a task or job to run at a specific time, day, and month. It is a powerful tool and is recommended for system administrators. They can schedule tasks for data backup, schedule updates, file synchronization, and many more. Crontab uses a daemon (a background process) which checks every minute to execute any scheduled task.

Each user has their crontab to create, modify, and delete tasks. By default, it is enable for all users but you can restrict someone by adding an entry in the /etc/cron.deny file.

In this helpful write-up, we will schedule the cron jobs in Red Hat Enterprise Linux 9.

Crontab Syntax

There are 6 fields in the crontab file which are explained below.

Minute – This is the first field which denotes the minutes in the range of 0-59.

Hour – This is the second field which denotes the hour in 0-23.

DoM Day of Month – This is the third field which denotes the day of the month between 0 and 31.

Month – This is the fourth field which denotes the month in the range 1-12 1 for January, 2 for February, and so on.

Day of week – This is the fifth and second last field in the range 0-7 which denotes the day of the week. 0 for Sunday, 1 for Monday so on and so forth.

User– User is the user name who wants to run the cron job.

Command – The command you want to execute.

Examples

Create a File

Let’s schedule a script for user karim to run at 12:30 PM, Wed Nov 29, and create an empty text file example.txt in the directory /home/karim.

For that fire up the terminal and open a crontab file located at /etc/crontab with nano editor.

sudo nano /etc/crontab -e

Update the file with the following content.

30 12 29 11 3 karim touch /home/karim/example.txt

Restart the crond service running in the background which checks the crontab file at regular intervals and runs the cron job.

sudo systemctl restart crond.service

Let’s verify if the cron service is up and running in the background.

sudo systemctl status crond.service

Let’s check the content of /home/karim by using ls command.

ls

You can see the file has been created at the path /home/karim.

Delete a File

In this second example, we are scheduling a cron job for user Karim which deletes the file example.txt located at /home/karim at 12:55 PM, Wed Nov 29.

Update the crontab file with the nano editor by using:

sudo nano /etc/crontab -e

The complete command will be:

55 12 29 11 3 karim rm /home/karim/example.txt

Restart the crond service.

sudo systemctl restart crond.service

Verify the directory /home/karim and example.txt won’t be there.

ls

Conclusion

We have taught you how you can schedule the cron jobs. Crontab is an efficient tool for sys administrators. They can use it in task automation. By now, we believe you have a good understanding of scheduling a cron job in Red Hat Enterprise Linux 9.