Job scheduling in Linux |Using crontab command

Hello, everyone In this Article we are going to learn how we can do Job scheduling using crontab in Linux in a very simple and easy step-by-step approach.

Job scheduling in Linux

Job scheduling in Linux

With the job scheduling, we can schedule the tasks that we need to perform daily on our server for example we need to take backup in the server / We need to monitor the services in the server we need to run scripts daily in our server.

Instead of hiring the system admin, we can do the job scheduling the benefit of this is that we can schedule the tasks automatically that is run daily on the server

The crontab command is used to schedule the task in the Linux kernel to process the task in the future.

1 How to check the package for crontab command

To check whether we have crontab command package or not we can run rpm command with -qa option

# rpm -qa crontab
Screenshot 1 15

2 How to check the status for crontab command

The crond is the demon of the crontab the command we need to check whether this is enabled or not.

# systemctl status crond
Screenshot 2 14

3 How to check the syntax for crontab command

To check the syntax for the crontab command then we need to view from /etc/crontab file.

# cat /etc/crontab
Screenshot 3 13

4 How to check current time and date

With the help of the date command we can see the current time and date in our system

# date
Screenshot 4 14

5 How to schedule a job using crontab

To schedule the job using crontab then we have to execute the crontab -e command,

Here we can get the blank file in which we can able to write the command which we want to schedule.

# crontab -e
Screenshot 5 14
Screenshot 7 12

6 How to check what job is added in crontab

To check which job is added with the crontab then we can execute crontab command with ( -l )option

# crontab -l
Screenshot 8 11

Now when we add ( * ) in the minute field then that job will execute in every minute

Screenshot 9 11

Now when we add ( */2 ) in the minute field then that job will execute in every 2 minute

Screenshot 10 10

With the above method we can schedule task for hours / days / weeks / month also

7 How to delete all the crontab

When we write ( -r ) option in the crontab command then we can delete the crontab.

Now when we execute # crontab -l command then crontab is no longer is there.

# crontab -r
Screenshot 11 11

8. How to add a crontab to specific user

To add crontab for the user then we can use the (-u ) option in the crontab command with the username and ( -e ) option.

# crontab -u devopstutuser -e
Screenshot 12 11

9. How to check logs for crontab

When we want to check the logs of crontab then we can execute the tail command with ( -f )option.

# tail -f /var/log/cron
Screenshot 13 9

Now for example we have made one script and that script we want to execute daily in a minute.

The location for the script is /root/daily.sh

Now We have created a script now we want to execute this script daily using crontab then we can add the script in the crontab file

Open the crontab file using # crontab -e command then add the path of the script with sh command

Screenshot 14 6

In companies we will not edit the crontab -e file there is the folder called as /etc/cron.hourly .

We can add our script here this script will execute every hour automatically

Screenshot 15 6

we can add a customized script here that will run every hour automatically. Just define the command here

Screenshot 16 5

Now we not want to define our script in the hourly folder we want to execute the script from a different location.

To do this go to cd /etc/cron.d directory here we can see there is a file named as 0hourly edit the location here.

There is the folder named as /mydir inside this we have our script that we need to execute for every hour.

In the hour .sh script we have defined the number of commands. we can set hour /minute here.

Screenshot 19 4
Screenshot 18 5
Screenshot 17 6

Similarly we have folders for daily / monthly / weekly / hourly

Screenshot 20 2

Now we want that the normal user will not able to execute the crontab then we can add the name of that user in # vi /etc/cron.deny file

Screenshot 21 2

Similarly we have file known /etc/cron.allows here in this file when we add the username we can allow that user to run the crontab

Screenshot 22 1

Please note that the allows file has priority over the deny file soo the username that is placed in the allows file has priority over the user that is placed in the deny file

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

If you want to read the manual for crontab then click on this link

This is the complete knowledge of how we can schedule the job using crontab in the Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment