How to schedule a Cron Job to run scripts on Rocky Linux 8.6

To Schedule A Cron Job To Run Scripts On Rocky Linux 8.6

Introduction

The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.

Installation Steps:

Step 1: Check the installed version of OS by using the below command

[root@linuxhelp ~]# cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.6 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.6 (Green Obsidian)"

Step 2: Check the crond status by using the below command

[root@linuxhelp ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2022-07-06 02:07:23 EDT; 11min ago
 Main PID: 22791 (crond)
    Tasks: 1 (limit: 17274)
   Memory: 1.1M
   CGroup: /system.slice/crond.service
           └─22791 /usr/sbin/crond -n

Step 3: Now create the .sh file by the name of sample.sh

[root@linuxhelp ~]# vim /mnt/sample.sh

mkdir /test
touch /file.txt

Step 4: Check the crontab format by using the below command

[root@linuxhelp /]# cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

#For details see man 4 crontabs
#Example of job definition:
#.---------------- minute (0 - 59)
#|  .------------- hour (0 - 23)
#|  |  .---------- day of month (1 - 31)
#|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
#|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
#|  |  |  |  |
#*  *  *  *  * user-name  command to be executed

Step 5: Create the entry in crontab by using the below command

[root@linuxhelp ~]# crontab -e
*	*               *       *       * sh /mnt/sample.sh

Step 6: Now list the crontab entry by using the below command

[root@linuxhelp ~]# crontab -l
* 	* 		*	*	* sh /mnt/sample.sh

Step 7: Now list the location / (before executing the command time 2.38) by using the below command

[root@linuxhelp ~]# cd /
[root@linuxhelp /]# ls -la
total 28
dr-xr-xr-x.  18 root root  252 Jul  6 02:25 .
dr-xr-xr-x.  18 root root  252 Jul  6 02:25 ..
lrwxrwxrwx.   1 root root    7 Oct 10  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jul  6 02:07 boot
drwxr-xr-x.  20 root root 3200 Jul  6 01:41 dev
drwxr-xr-x. 150 root root 8192 Jul  6 02:07 etc

-rw-r--r--.   1 root root    0 Jul  6 02:38 file.txt
drwxr-xr-x.   3 root root   23 Jun 27 08:48 home
lrwxrwxrwx.   1 root root    7 Oct 10  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Oct 10  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Oct 10  2021 media
drwxr-xr-x.   3 root root   35 Jul  6 02:24 mnt
drwxr-xr-x.   2 root root    6 Oct 10  2021 opt
dr-xr-xr-x. 243 root root    0 Jul  6 01:41 proc
dr-xr-x---.  17 root root 4096 Jul  6 02:24 root
drwxr-xr-x.  47 root root 1340 Jul  6 02:07 run
lrwxrwxrwx.   1 root root    8 Oct 10  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Oct 10  2021 srv
dr-xr-xr-x.  13 root root    0 Jul  6 01:41 sys
drwxr-xr-x.   2 root root    6 Jul  6 02:25 test
drwxrwxrwt.  26 root root 4096 Jul  6 02:37 tmp
drwxr-xr-x.  13 root root  158 Jun 27 08:44 usr
drwxr-xr-x.  22 root root 4096 Jul  5 03:23 var

Step 8: Now list the location / (after executing the command time 2.39) by using the below command

[root@linuxhelp /]# ls -la
total 28
dr-xr-xr-x.  18 root root  252 Jul  6 02:39 .
dr-xr-xr-x.  18 root root  252 Jul  6 02:39 ..
lrwxrwxrwx.   1 root root    7 Oct 10  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jul  6 02:07 boot
drwxr-xr-x.  20 root root 3200 Jul  6 01:41 dev
drwxr-xr-x. 150 root root 8192 Jul  6 02:07 etc
-rw-r--r--.   1 root root    0 Jul  6 02:39 file.txt
drwxr-xr-x.   3 root root   23 Jun 27 08:48 home
lrwxrwxrwx.   1 root root    7 Oct 10  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Oct 10  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Oct 10  2021 media
drwxr-xr-x.   3 root root   35 Jul  6 02:24 mnt
drwxr-xr-x.   2 root root    6 Oct 10  2021 opt
dr-xr-xr-x. 243 root root    0 Jul  6 01:41 proc
dr-xr-x---.  17 root root 4096 Jul  6 02:24 root
drwxr-xr-x.  47 root root 1340 Jul  6 02:07 run
lrwxrwxrwx.   1 root root    8 Oct 10  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Oct 10  2021 srv
dr-xr-xr-x.  13 root root    0 Jul  6 01:41 sys
drwxr-xr-x.   2 root root    6 Jul  6 02:39 test
drwxrwxrwt.  26 root root 4096 Jul  6 02:38 tmp
drwxr-xr-x.  13 root root  158 Jun 27 08:44 usr
drwxr-xr-x.  22 root root 4096 Jul  5 03:23 var

Step 9: Now remove the directory and file by using the below command

[root@linuxhelp /]# rm -rvf test/ file.txt 
removed directory 'test/'
removed 'file.txt'

Step 10: Now list the location (time 3.07) by using the below command

[root@linuxhelp /]# ls -la
total 28
dr-xr-xr-x.  17 root root  224 Jul  6 03:07 .
dr-xr-xr-x.  17 root root  224 Jul  6 03:07 ..
lrwxrwxrwx.   1 root root    7 Oct 10  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jul  6 02:07 boot
drwxr-xr-x.  20 root root 3200 Jul  6 01:41 dev
drwxr-xr-x. 150 root root 8192 Jul  6 02:07 etc
drwxr-xr-x.   3 root root   23 Jun 27 08:48 home
lrwxrwxrwx.   1 root root    7 Oct 10  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Oct 10  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Oct 10  2021 media
drwxr-xr-x.   3 root root   35 Jul  6 02:24 mnt
drwxr-xr-x.   2 root root    6 Oct 10  2021 opt
dr-xr-xr-x. 244 root root    0 Jul  6 01:41 proc
dr-xr-x---.  17 root root 4096 Jul  6 02:24 root
drwxr-xr-x.  47 root root 1340 Jul  6 02:07 run
lrwxrwxrwx.   1 root root    8 Oct 10  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Oct 10  2021 srv
dr-xr-xr-x.  13 root root    0 Jul  6 01:41 sys
drwxrwxrwt.  27 root root 4096 Jul  6 03:07 tmp
drwxr-xr-x.  13 root root  158 Jun 27 08:44 usr
drwxr-xr-x.  22 root root 4096 Jul  5 03:23 var

Step 11: List the location after 1 minute 03.08 by using the below command

[root@linuxhelp /]# ls -la
total 28
dr-xr-xr-x.  18 root root  252 Jul  6 03:08 .
dr-xr-xr-x.  18 root root  252 Jul  6 03:08 ..
lrwxrwxrwx.   1 root root    7 Oct 10  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jul  6 02:07 boot
drwxr-xr-x.  20 root root 3200 Jul  6 01:41 dev
drwxr-xr-x. 150 root root 8192 Jul  6 02:07 etc
-rw-r--r--.   1 root root    0 Jul  6 03:08 file.txt
drwxr-xr-x.   3 root root   23 Jun 27 08:48 home
lrwxrwxrwx.   1 root root    7 Oct 10  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Oct 10  2021 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Oct 10  2021 media
drwxr-xr-x.   3 root root   35 Jul  6 02:24 mnt
drwxr-xr-x.   2 root root    6 Oct 10  2021 opt
dr-xr-xr-x. 244 root root    0 Jul  6 01:41 proc
dr-xr-x---.  17 root root 4096 Jul  6 02:24 root
drwxr-xr-x.  47 root root 1340 Jul  6 02:07 run
lrwxrwxrwx.   1 root root    8 Oct 10  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Oct 10  2021 srv
dr-xr-xr-x.  13 root root    0 Jul  6 01:41 sys
drwxr-xr-x.   2 root root    6 Jul  6 03:08 test
drwxrwxrwt.  26 root root 4096 Jul  6 03:07 tmp
drwxr-xr-x.  13 root root  158 Jun 27 08:44 usr
drwxr-xr-x.  22 root root 4096 Jul  5 03:23 var

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to schedule a Cron Job to run scripts on Rocky Linux 8.6. Your feedback is much welcome.

FAQ
Q
Is it necessary to restart crond service after editing crontab for any user?
A
No, it is not necessary to restart crond service after editing crontab for any user.
Q
Where do the logs related to cron jobs collected?
A
In the file /var/log/cron.
Q
What will happen if we delete /etc/cron.deny and /cron.allow files?
A
Only root users will be able to schedule cron jobs.
Q
What is the name of the service associated with cron jobs?
A
The name of the service associated with cron jobs is crond
Q
How to execute a Linux cron job every second using Crontab?
A
You cannot schedule an every-second cronjob. In cron, the minimum unit you can specify is a minute. In a typical scenario, there is no reason for most of us to run any job every second in the system.