How to use Cron to automate tasks in Ubuntu 22.04/Debian 11
Cron is a tool used to automate tasks in Unix-based operating systems, such as Ubuntu 22.04 and Debian 11. With Cron, you can schedule commands to run automatically at specific times of the day, week, or month.
In this manual, we will explain how to use Cron in Ubuntu 22.04 and Debian 11 to automate tasks simply and efficiently.
Step 1: Access the console
First you have to access the console or terminal of Ubuntu or Debian. To do this, open the Terminal application in your operating system.
Step 2: Access the Cron file
The Cron file is the file that contains the configuration of the tasks you want to automate. To access the Cron file, type the following command in the console:
crontab -e
This command will open the Cron file in the default text editor of your operating system.
Step 3: Configure the task you want to automate
Once you have accessed the Cron file, you can begin to configure the task you want to automate. Each line of the Cron file represents a different task that will run at a specific time. Each line has five different fields that you must configure:
* * * * * command_to_run
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday = 0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- minute (0 - 59)
For example, if you want to configure a task to run every day at 6:30 am, the configuration line would look like this:
30 6 * * * command_to_run
In this example, '30' represents the minute, '6' represents the hour and the five asterisks represent the day of the week, the month and the day of the month, respectively.
Step 4: Save the configuration
Once you have configured the task you want to automate, you must save the configuration in the Cron file. To do this, save the changes to the file and close it. The Cron file will be saved automatically.
Step 5: Verify the configuration
To verify that the task has been configured correctly, type the following command in the console:
crontab -l
This command will show you a list of all the tasks you have configured in the Cron file.
Step 6: Verify the task execution
Once the task has been configured in Cron, you must wait for it to run at the scheduled time. You can verify if the task has run correctly by consulting the system logs.
To view the system logs, type the following command in the console:
grep CRON /var/log/syslog
This command will show you a list of all the tasks that have run in Cron, along with the time and date of the execution.
success With these steps, you should be able to automate tasks easily.