Scheduling important jobs with CRONTAB : Crontab Examples

Crontab is very important utility in Linux systems & is used to set up time based job scheduling.  With the help of Crontab or cron, we can execute a command or a script to run at a particular time & we will discussing just that in this tutorial, with the help of some crontab examples.

For example , let’s say you created a script for taking backup & you need to run the script daily at 11:00 PM . For one or two systems, we can run the script manually, it might not be an issue but if we are having 10-20 systems or even more it will be a problem. So instead, we will use ‘crontab‘  to create an cron–job that will run the script automatically daily at 11:00PM.

(Recommended Read: How to replicate SELinux policies among Linux machines )

(Also Read: Complete monitoring solution : Install OMD (Open Monitoring Distribution))

Now let’s see some commands & then we will discuss some Crontab examples,

Crontab Examples

• Add a new cron-job

To edit or create a cronjob , run

$ crontab –e

 

• Display all cron-jobs

$ crontab –l

 

• Removing a cron-job

$ crontab –r

Or we can also prompt a user before deleting an item by

$ crontab –ir

 

Syntax for using Crontab

(minute) (hour) (day of month) (month) (day of week) (command/script path)

 

Examples of crontab

1- Running a backup script named bkp.sh located at /etc/script everynight at 11:00PM,

00 23 * * * /etc/script/bkp.sh

 

2- Running a weekly backup script named week.sh on every Friday at 11:30,

30 23 * * 5 /etc/script/week.sh

 

3- Running a monthly backup script named monthly.sh on 1st day of every month at 11:45PM,

45 11 1 * * /etc/script/monthly.sh

 

4- Running backup script named weekday.sh on weekdays only at 11:59 PM

59 11 * * *1-5 /etc/script/weekday.sh or
59 11 * * *1 2 3 4 5 /etc/script/weekday.sh

 

5- Running top command every 15 minutes to performance monitoring

* /15 * * * top

 

Crontab is pretty easy to use and will make life of sysadmin much easier once you learn to make full use of it.
That’s it for now, we now end our tutorial on using CRONTAB, here we have discussed some crontab examples but there are plenty more use cases. If you have any queries/suggestions, feel free to use our comment box down below.

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter –

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

2 Responses

  1. ajhay says:

    Very helpfull thanks for this article.

Leave a Reply

Your email address will not be published. Required fields are marked *