How to configure and execute cronjob in linux with practical examples


How to configure and execute crontab in linux with practical examples

In this article we learn what is crontab ,How to configure crontab ,How to listing crontab ,How to backup crontab to restore it if you accidentally delete crontab and we also demonstrate with some practical real time examples how to schedule a job using various option of crontab.

How to install crontab in linux system step by step guide

What is crontab

Crontab is a very useful utility for a system administrator for running a specific task on a regular time frame. Crontab execute any job in the  backend of the system to perform some routine tasks like daily backup, generation of system health report on regular interval.
In the crontab option six fileds are there.

  • First field => to indicate minutes.
  • Second filed => to indicate hours.
  • Third field=> to indicate day of month.
  • Fourth filed=> to indicate month of year.
  • Fifth filed=> to indicate day of week.
  • Sixth field=> to indicate command or script which is to be executed.


How to add crontab to execute a task automatically


We can schedule a task on a regular interval using crontab utility. To add a specific task to execute automatically on a regular interval we can do it using the below command.

#crontab –e

For a particular user we can set crontab using the below command.

#crontab –u username –e

How to list crontab which we have added to execute automation task

After adding crontab we need to verify the crontab using the below command.

#crontab –l

For a particular user we can list crontab using the below command.

#crontab –u username –l

 
How to configure and execute crontab in linux with practical examples
How to configure and execute crontab in linux with practical examples

How to backup crontab

We have to take crontab backup, if we delete all crontab entries accidentally then it will be very useful to recover all crontab entries. To backup crontab we use the below command.

#crontab -l > backup.txt

To remove all crontab enties we use the below command.

#crontab –r

To restore all crontab entries we use the below command.

#crontab backup.txt

After executing the above command all crontab entries will be restoared.

Some practical example for crontab

  •          If we want to execute a task at 12 pm daily we use the below syntax to execute the task.


          * 12 * * * df -h

  •         If we want to execute a task every 1 hour daily we use the below syntax to execute the task.


          * */1 * * * df –h

  •          If we want to execute a task every 10 minutes daily we use the below syntax to execute the task.


         */10 * * * * df –h

  •         If we want to execute a task twice of a day we use the below syntax to execute the task.


        * 12,23 * * * df –h
        It will be schedule for 12 pm and 11 pm.

  •          If we want to execute a task Sunday at 2 pm we use the below syntax to execute the task.


        * 14 * * 7 df -h


  •         If we want to execute a task february we use the below syntax to execute the task.


       * * * feb * df -h

  •          If we want to execute a task 30th of a month  we use the below syntax to execute the task.


       * * 30 * * df –h




SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment