How to Schedule and Automate Tasks with Crontab in Ubuntu

Tired of having to do some tasks on your computer manually yourself? If you are using Linux or Ubuntu, these manual tasks should be fine because you can easily schedule tasks.

In this article, we discussed how to use Crontab to schedule and automate tasks in Ubuntu. Please note that although Ubuntu is taken as an example here, the following steps apply to any Linux distribution.

Schedule and automate tasks with crontab in Ubuntu

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

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

Crontab job arrangement basis

m h dom mon dow user command
┬┬┬┬┬
││││││
││││││
││││└───── Day of the week (0-6) (0 is Sunday, or use name)
│││└────────── Month (1 – 12)
││└────────────── Day of the month (1 – 31)
│└────────────────Hour (0 – 23)
min──────────────────Minutes (0-59)

  • Meter – Stands for minutes and can be any number between 0 and 59.
  • H – Indicates the hour, which can be any number between 0 and 23.
  • dom – Indicates the day of the month, which can be any number between 1 and 31.
  • on Monday – Represents the month. You can explicitly set the month to run the task. The range is 1 to 12.
  • Dow – Do you want the task to run on specific days of the week? You can choose a number between 0 and 6.
  • user – If there are multiple users on Ubuntu, you can specify which user is responsible for the task.
  • command – After selecting the time and user account, you need to write a command for the task itself.

How to schedule backups on Ubuntu using Crontab

After explaining the tools that will be used to automate tasks in Ubuntu, here’s how to schedule backup tasks:

1. Launch the terminal from the application menu or press Ctrl + Alt + T.

2. Type the command:

crontab -e

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

4. In the open crontab file, use the down arrow key to scroll down to the end of the file. To add a task to run at a specific time, add the task in the following format:

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

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

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

Scheduling tasks in Ubuntu 2

5. When finished, press Ctrl + O to save the file in the nano editor. Press Ctrl + X to exit Nano editor.

That’s it.

in conclusion

With these instructions, you can easily schedule tasks in Ubuntu. Would you like to see more tips on using Ubuntu? Check out our guide on how to start Ubuntu in recovery mode.

Ranch
                        
                    
                    Ranch

Sidebar