In this tutorial we will learn to use an important Linux utility named ‘Crontab’. With the help of Crontab or cron, we can execute a command or a script to run at a particular time.
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.
Now let’s see some commands & then we will discuss some examples of crontab
• 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. If you have any queries/suggestions, feel free to use our comment box down below.