Linux Series: Understanding Cron in Ubuntu
Tags: linux, cloud, beginners, tutorial
Published: 2024-03-08 16:49:41

One of the notable things computer technology has allowed humanity to do since the beginning is to automate tasks with little or no babysitting while still being productive.

In this article, I will teach you how to use Cron to automate processes and jobs (remember those from the last article? Check it out [here](https://dev.to/olodocoder/linux-series-full-guide-managing-processes-in-ubuntu-2npf) 🫵🏾) so you don't have to be there when they run and don't need to have it running in the background all the time (you want to save some of those resources $$ for the greater good, don't you? 😉).

> For this article, I have set up an Ubuntu desktop virtual machine on my system. However, you should be able to follow along with any Linux system you have, either virtual or physical.

Let's get into it!

## Cron
Cron is a time-based job scheduler in Unix-like operating systems. It enables you to schedule tasks (commands or scripts) to run periodically at fixed times, dates, or intervals. These tasks can be anything from system maintenance to backups or scripts to automate specific processes.

Cron operates through a cron daemon, which runs continuously in the background, checking for scheduled tasks at specified intervals. When a scheduled time or interval matches the current time, the cron daemon executes the associated task.

Now that you know what Cron is and how it works, let's get into how to check your `crontab`.

## Cron Table `crontab`
To use Cron, you typically edit a file called the `crontab` (cron table), which contains the list of scheduled tasks for a particular user. Each user on a system can have their crontab file.

To view your `crontab`, you can use the following command:
```bash
crontab -l
```
The code above should return your `crontab`, but I don't have any, so I'll see this:

![crontabs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebivu2vwii04cdidarmf.png)
 
Or check for other users with this command:
```sh
crontab -l -u username
```
Replace `username` with the username of the user whose `crontab` you want to view. This command lists the scheduled tasks for the specified user.

> Note: You need access to `sudo` for the command above to work.

## How to Use Cron
You can start using Cron by editing your `crontab` using the following command:
```
crontab -e
```
This command opens the crontab file in your default text editor (usually Vim or Nano), allowing you to add, edit, or remove scheduled tasks.

You can also specify the editor you'd like to use to edit the `crontab` like so:
```sh
EDITOR=nano crontab -e
```
The command above will open `crontab` with the specified editor.

## Editing Cron Tables
Editing your `crontab` is relatively straightforward. You only have to specify the time and provide a command or the path to the script you want to run at the specified time.

Specifying the time can be done using the five asterisks (`*`). In the cron syntax, the five asterisks represent the time and date fields used to specify when a task should be executed. 

Each asterisk corresponds to a specific time unit. Let's break it down:

```sh
* * * * *
```
1. **Minute (0-59):** The first asterisk represents the minute field. In this position, an asterisk means "every minute". So, the task will be executed every minute of every hour.

2. **Hour (0-23):** The second asterisk represents the hour field. Like the minute field, an asterisk here means "every hour". So, the task will run every hour of every day.

3. **Day of Month (1-31):** The third asterisk represents the day of the month field. Once again, an asterisk means "every day of the month". Therefore, the task will execute every day, regardless of the date.

4. **Month (1-12):** The fourth asterisk represents the month field. Just like the previous fields, an asterisk means "every month". So, the task will run every month of the year.

5. **Day of Week (0-7, where both 0 and 7 represent Sunday):** The fifth asterisk represents the day of the week field. Here, an asterisk signifies "every day of the week". Therefore, the task will execute every day of the week.

Putting it all together, specifying `* * * * *` means that the task specified in the `crontab` job will run every minute, every hour, every day, every month, and every day of the week, effectively executing it continuously.

Let's explore different examples next.

## Common Commands and Examples:
Let's look at some common times and task examples and how to specify them in `crontab`:

### Minute, Hour, Day of Month, Month, Day of Week
   ```sh
   * * * * * command_to_run
   ```
This runs `command_to_run` every minute, every hour, every day of the month, and every day of the week.

### Specific Time
   ```sh
   30 3 * * * command_to_run
   ```
This runs `command_to_run` at 3:30 AM every day.

### Interval
   ```sh
   */15 * * * * command_to_run
   ```
This runs `command_to_run` every 15 minutes.

### Using Paths
   ```sh
   0 1 * * * /path/to/command_to_run
   ```
This runs `command_to_run` located at `/path/to/` at 1:00 AM daily.

### Redirecting Output
   ```sh
   0 2 * * * command_to_run >> /path/to/logfile.log 2>&1
   ```
This runs `command_to_run` at 2:00 AM every day and appends its output, including any errors, to `logfile.log`.

### Running Scripts
   ```sh
   0 4 * * * /path/to/script.sh
   ```
This runs a script named `script.sh` located at `/path/to/` at 4:00 AM daily.

After editing your crontab file, the changes will take effect immediately, and the cron daemon will execute the scheduled tasks accordingly.

Remember to check the syntax carefully, as incorrect entries in the crontab file can lead to unexpected behavior or errors. Additionally, always ensure that the commands or scripts you schedule are tested and do not interfere with system stability or security.

## Conclusion
And that's it! I hope this article achieved its aim of teaching you everything you need to know to start using Cron to automate tasks on your Linux server.

You also explored typical times and tasks in `crontab`, how to specify them, and more.

Please feel free to leave a comment to correct, suggest, or even teach me something new! 😉 

Finally, remember to follow me here on Dev, [LinkedIn](https://www.linkedin.com/in/adams-adebayo/), and [Twitter](x.com/olodocoder). Thank you so much for reading, and I'll see you in the next one!