Don’t repeat yourself system repeats itself for you.

Ajesh Kalayil
3 min readAug 10, 2021

--

One thing you need to know all about make doing things right at right time with the right tool.

Photo by Joanjo Pavon on Unsplash

Today morning I was in a discussion with a colleague who used to backup the production database on daily basis using scripts. I was wondered does this happens in 2021?. That will probably be a headache at least this work from home time. What happens if you will take a bit more time to prepare a delicious breakfast for your special ones. Who will execute the script and get the backup on time?

No worries Linux crontab will do it for you!

Covering all the aspects of the crontab is not the scope of this article.
That is a bit longer. If interested to hear more about crontab, let me know in the comment box.

Intended audience

  • Linux users
  • Tech enthusiast

Before diving into the core, let’s ask three questions

  1. What is crontab
    A crontab file is a simple text file containing a list of commands meant to run at a specified interval of times. Cron is a system daemon used to execute desired tasks in the background at scheduled times by reading the crontab file.
  2. Why crontab
    The crontab is helping to automate backups or any repetitive tasks that should do in a particular interval of time. We can configure the job and schedule through the crontab file.
  3. When crontab
    If you have any tasks that may be database backup or any other repetitive tasks that should do in particular intervals of time, crontab is there for you.

Crontab checklists

  • Add the user to crone tab groups.
  • Open the crontab in your favorite editor.
  • Schedule the job in the crontab file, save & close the editor.

How to schedule a job on crontab

Scheduling a job on crontab is pretty straightforward. Let’s be thankful to the vendor for such simplicity.

Let’s follow our checklist, don’t worry too much. We will step in simple as possible.

Add the user to the crontab group.

$ sudo usermod -a -G crontab <username>

Open the crontab editor using your favorite editor.

$ crontab -e

To schedule a job on the crontab file, make entries in the below format.

.---------------- 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
* * * * * <command to run>
<minute> <hour> <day of month> <month> <day of week> <command to run>

Let’s schedule few database backup jobs using db_backup.sh at various time intervals to get an idea of the concept.

  1. 10:15 am daily.
15 10 * * * /path/to/db_backup.sh

2. Every Monday of the week/month at 2.30 pm.

30 14 * * 1 /path/to/db_backup.sh

3. Every first four days of the month at 10:45 pm (Range).

45 22 1-4 * * /path/to/db_backup.sh

4. Every 1st,3rd,6th and 15th days of the month at 9.00 am.

0 9 1,3,6,15 * * /path/to/db_backup.sh

5. Every Wednesday and every first 3 days of the month at 12 am.

0 0 1-3 * 3 /path/to/db_backup.sh

NOTE: If in case we specify both ‘days of the month’ and ‘days of the week’ values other than an asterisk in the crontab file. Both are taken into account while matching.

Special strings

Instead of the first five fields, we can use the special strings shown below for readability and save time.

  • @reboot — Run once, at the start-up.
  • @yearly, @annually — Run once a year ( 0 0 1 1 * ).
  • @monthly — Run once a month ( 0 0 1 * * ).
  • @weekly — Run once a week ( 0 0 * * 0 ).
  • @daily, @midnight — Run once a day ( 0 0 * * * ).
  • @hourly — Run once an hour ( 0 * * * * ).

Final thoughts

  1. To view user crontab file — $ crontab -l
  2. To remove user crontab file — $ crontab -r
  3. To add custom crontab file — $ crontab /path/to/file

Takeaway

Thank you all for letting me share my knowledge with you. Have a good day ahead.

Kindly let me know your feedback/correction/doubts in the comment box.

--

--