Crontabs in Linux – Easy Reference

In this tutorial, we will be learning how to schedule tasks with crontabs in Linux. Crontab stands for cron table which contains a list of tasks which are scheduled to run at a regular interval of time. These tasks are then executed by a daemon called cron. All users on the machine can have their separate crontab that helps them to schedule tasks. So let’s get started!

How to create and edit crontabs in Linux?

If you have never used crontabs before, you need to create a crontab file which will contain the details of all the scheduled tasks. This file can simply be created by the following command:

crontab -e

This will now prompt you to select the editor of your like. Just as soon as you select the editor, your crontab file will open up. Here in the example below I have selected the nano editor.

Opening Crontab File On Nano
Opening Crontab File On Nano

The same command (crontab -e) that we used to create the file will also be used, from now on, to edit this file.

How to add entries to the crontab file?

Once the crontab file is created now all we have to do is add entries to this file. Now, cron entries have a specific format having 6 fields with a general syntax something like this:

minute(s) hour(s) day(s) month(s) weekday(s) command(s)
Fields Values
Minutes0-59
Hour0-23
Day1-31
Month 1-12
Weekday0-6 (where 0: Sunday, 1: Monday and so on)
CommandComplete Executable command

You can also use asterisk (*) in any of these fields to set it to “first to last”. So if the day field is set to *, the command will be executed every day. You can also add multiple entries in a field with the help of a comma (,). You can also put a range of values using the dash (-) symbols. Now let’s jump into our first example. Here I am scheduling a task to execute every week on Monday at 12:00.

0 12 * * 1 nano /home/dheeraj/file.txt

This is what it looks like:

Crontab Entry
Crontab Entry

Now let’s take another example where I want to schedule a task on 16:30 every 5th,10th, and 15th day of every month, I would put the following entry:

30 16 5,10,15 * * /bin/sh /home/dheeraj/task.sh

In the following example, I am scheduling task on 12:30 on every 1-5 day of the month:

30 12 1-5 * * touch /home/dheeraj/NewFile.txt

Crontab entries could be slightly confusing at times. You can also use Crontab Guru, it’s a great online utility that lets you check your cron entries.

How to delete crontab entries?

First to check existing crontab entries, you can run the following command:

crontab -l

To clear out all the crontab entries you can run the following command:

crontab -r
Removing Crontab Entries
Removing Crontab Entries

How to change crontab entries for other users?

If you are root, you can change crontab entries for other users as well using the -u option. So for example if you wish to edit cron files for the user “shyam” as root you would be running the following command:

crontab -u shyam -e

Conclusion

I hope you learnt how to use crontab from this tutorial. It might be slightly tricky at first but eventually you will get the hang of it. Be sure to check the man page (man crontab) and help page (crontab -h) of crontab if you’re stuck. And thank you so much for reading this article. Have a productive day ahead! Cheers!