How to Use Crontab in Linux

One of the basic operations in any server is the automation of tasks. This, in Linux, is possible thanks to Crontab. So, if you have wanted to program tasks or scripts in this post you will learn how to do it because the goal is that you know how to use Crontab in Linux.

Cron vs Crontab? What are they?

Cron is a background process that runs from the moment you start your system. The main function of Cron is to search the entire system for scheduled tasks to be executed at the exact time defined.

As expected, a basic condition for Cron to work is that the time zone and the system clock is correctly configured.

But also another concept arises and it is that of Crontab that is simply a text file where we will be able to establish tasks that the system will do using cron.

This file is something special and is invoked in the form of a command and has some options that help modify its behavior.

Using Crontab in Linux

Crontab is available in all Linux distributions so you only need to open a terminal session.

And to check if it is available you just have to show the help:

crontab --help

And you will get a screen output like this:

usage: crontab [-u user] file
crontab [ -u user ] [ -i ] { -e | -l | -r }
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)

So, it is quite simple to use Crontab. And to start adding tasks to Cron, just run:

crontab -e

So, if there is no Crontab created for the user, you will get a screen output similar to this one:

no crontab for user

And immediately the default text editor will be opened in the terminal.

However, if you want to force Crontab to use nano, you can execute this command:

export VISUAL=nano; crontab -e

And so you can use nano which is easier to use.

In the final part of the file, you’ll see the basic syntax to add a task that is

# m h dom mon dow user command

I will explain below what each of the options refers to:

  • m: The minute when the task will be executed.
  • h: The exact time when the task will be executed.
  • dom: Day of the month when the task will be executed.
  • dow: Day of the week. Where 0 and 7 are Sunday and so on. Also, you can specify the first 3 letters of the day for example mon, tue,wed…
  • user: the user that will execute the task.
  • command: command or script to execute.

Now I will provide you with some examples of some tasks so you can see how to add a task correctly.

Some examples of the use of Crobtab in Linux

Crontab’s syntax, although it may seem cumbersome, is not. It is quite simple to follow but it is convenient than to give some examples to better illustrate what I am saying.

00 10 * * 0 root apt-get -y update

This task will refresh the repositories every Sunday at 10:00 am.

But you can also specify a script to be executed. Suppose there is a script called commands.sh and it has to be executed by the root user.

00 10 * * 0 root /home/user/commands.sh

As you can see, since it is a script, you have to specify the absolute path of the script including its name. Also, you have to assign it execution permissions with the chmod command because otherwise, it will not be able to be executed.

Another example:

30 2 30 11 * user /home/user/scripts/commands.sh

This task will cause a script called commands.sh located in the scripts folder inside the home folder to be executed. This script will run every November 30th at 2:30 AM

When the field is filled in with * we are indicating that it does not matter that value and that it can take any value. We see this example, where the day of the week does matter:

30 2 30 11 sun user /home/user/scripts/commads2.sh

This task is conditioned by the day of the week. In this case, it will be executed every November 30th at 2:30 am as long as that day is Sunday.

Also, in organizations you may need to execute tasks at the end of the working day, an example of this may be this task:

30 17 * * 1,2,3,4,5 user commands.sh

In this case, I have added commas to the days of the week when the script will be executed. The commands.sh script will be executed every Monday, Tuesday, Wednesday, Thursday, and Friday at 17:30.

And so you can add tasks without problems.

Crontab options in Linux

Once you have defined the tasks you want to be executed automatically, it is time to save the file like any other.

On the other hand, other options allow you to better manage crontab.

crontab [file]

This command replaces the system crontab file with another one we specify.

crontab -l

Lists all crontab tasks of the current user.

crontab -d

Delete the user’s crontab.

This way you can make the changes you need.

Conclusion

Crontab allows us to quickly and easily program tasks in our system. This allows us to automate common tasks such as making backups or planning system cleanups. Or even perform operations within the operating system such as installing or removing unnecessary programs.

If you want more information, you can consult the official documentation of Crontab.

Have you used crontab? Do you like it? have you tried it? leave us a comment and share this post.

Scroll to Top