How to Schedule and Automate Tasks with Crontab in Ubuntu

Schedule Task Ubuntu Featured

Tired of having to manually handle certain tasks on your computer by yourself? If you are using Linux or Ubuntu, these manual tasks shouldn’t be a problem, as you can easily schedule tasks.

In this article we discuss the use of Crontab to schedule and automate tasks in Ubuntu. Do note that while we are using Ubuntu as an example here, the steps below will work for any Linux distribution.

Using Crontab to Schedule and Automate Tasks in Ubuntu

The Cron daemon performs the same function as Task scheduler on Windows. With this application, you can choose a preferred time for any process you want to start, whether it is a backup or maintenance task. With this utility, you can schedule a task without manual intervention.

However, before delving into the use of Crontab, it is important to understand the structure and arrangement for configuring jobs on it.

Crontab Job Arrangement Basics

m h dom mon dow user command
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 – 6) (0 is Sunday, or use names)
│ │ │ └────────── month (1 – 12)
│ │ └─────────────── day of month (1 – 31)
│ └──────────────────── hour (0 – 23)
└───────────────────────── min (0 – 59)

  • m – represents minute and can be any number from 0 to 59.
  • h – represents hour and can be any number from 0 to 23.
  • dom – represents the day of the month and can be any number between 1 to 31.
  • mon – represents months. You can explicitly set the month you want a task to run. The range is 1 through 12.
  • dow – do you want a task to run on a specific day of the week? You can choose a number between 0 and 6.
  • user – if you have more than one user on Ubuntu, you can specify which one is responsible for the task.
  • command – after choosing the time and user account, you need to write out a command for the task itself.

How to Use Crontab to Schedule a Backup on Ubuntu

Having explained the tools you will be using to automate tasks in Ubuntu, here’s how to schedule a backup task:

1. Launch a terminal, either from the Applications menu or by pressing Ctrl + Alt + T.

2. Type the command:

crontab -e

3. If you are running it for the first time, it will ask you to choose the editor for opening the file. You can press 2 for nano.

Schedule Tasks In Ubuntu 4

4. In the crontab file that opened, scroll down to the end of the file with the Down arrow key. To add a task to run at a specific time, add your task in the following format:

m    h   dom   mon dow /file/path/to/command

For example, to run a backup script at 5am every Monday:

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

Schedule Tasks In Ubuntu 2

5. When you are done, press Ctrl + O to save the file in the nano editor. Press Ctrl + X to exit the nano editor.

That’s it.

Conclusion

With these instructions it will be quite easy for you to schedule tasks in Ubuntu. Would you like to check out more tips on using Ubuntu? Check out our guide on how to boot Ubuntu in recovery mode.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sarah Adedun

Sarah Adedun is a technology enthusiast. When she is not reviewing tech products, you can find her sharing personal thoughts on Medium and researching ways to merge Finance and Technology together.