> For the complete documentation index, see [llms.txt](https://devops-3.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devops-3.gitbook.io/devops/docs/module-1-linux-introduction/cron-jobs.md).

# Cron Jobs

A Cron Job is a Linux program that allows users to schedule the execution of a piece of software, often in the form of a shell script or a compiled executable.&#x20;

Cron is typically used when you have a task that needs to be run on a fixed schedule, and/or to automate repetitive tasks like downloading files or sending emails.

At its most basic level, a cron job is an entry written into a table called the *cron table*, otherwise known as the *crontab* for short. This entry contains a schedule and a command to be executed.&#x20;

The cron daemon (*crond*) looks for entries in the crontab to determine what jobs it should run, and when it should run them according to the specified schedule.

### How Do Cron Jobs Work? <a href="#how-do-cron-jobs-work" id="how-do-cron-jobs-work"></a>

Most standard installations of cron consists of two commands:

* `cron` or `crond`, which is the daemon that runs the scheduling utility
* `crontab`, which is the command that allows you to edit the cron entries for your jobs

When you talk about a *daemon* from a Linux perspective, it’s a program that is running in the background and is noninteractive.&#x20;

This means that the program does not accept any user input and doesn’t display output to the user.&#x20;

The word *daemon* is historically used in a Unix/Linux context and is not a universal term across different operating systems.

The daemon will be running under the *root* user. You can run the following command to see if cron is running:

```bash
$ ps aux | grep cron
root     	617  0.0  0.0   9420  2800 ?    	Ss   17:00   0:00 /usr/sbin/cron -f
```

### Cron Job Schedule Syntax <a href="#cron-job-schedule-syntax" id="cron-job-schedule-syntax"></a>

A basic crontab entry looks something like this, with the cron job schedule first, followed by the command to run:

```bash
*    *    *    *    *   /home/user/bin/somecommand.sh
|    |    |    |    |            |
|    |    |    |    |    Command or Script to execute
|    |    |    |    |
|    |    |    | Day of week(0-6 | Sun-Sat)
|    |    |    |
|    |    |  Month(1-12)
|    |    |
|    |  Day of Month(1-31)
|    |
|   Hour(0-23)
|
Min(0-59)
```

An asterisk (\*) matches all values, so if you take a look at the example above, it specifies that **/home/user/bin/somecommand.sh** should be run at *minutes* 0-59 and *hours* 0-23 on *days of the month* 1-31 for *months* 1-12 on *days of week* 0-6 — or in other words "[every minute](https://crontab.guru/#*_*_*_*_*)".

### Crontab Operators

Cron syntax also utilizes operators for performance. Operators are large inland that operates effectively on the Cron attribute values.

1. **Asterisk operator (\*)**

The asterisk operator denotes any significance or already. Suppose you see an asterisk (\*) in the Hour domain, it implies the job will be done every hour. It represents all values.&#x20;

Utilization of this operator is to gather operating for the entire month or week.

2. **Comma Operator (,)**

You can stipulate a range of items for regurgitation using the comma operator. It also defines distinct unique values.

3. **Hyphen Operator (-)**

You can stipulate a set of outcomes using the hyphen operator. If you enter 2-5 in the Weekday domain, the assignment will execute every weekday (From Tuesday to Friday). It also represents a set of parameters.

4. **Forward slash Operator (/)**

The slash operator allows you to specify values that will be repeated over a specific interval between them. This operator can also be used to separate a number into various stages.

### Managing Crontab Entries <a href="#managing-crontab-entries" id="managing-crontab-entries"></a>

Once cron is running, it checks for crontab entries in the following files every minute:

* **/etc/crontab**
* **/var/spool/cron/crontabs/$USER** (where `$USER` is the currently logged-in user).
* From the terminal, enter *edit* mode for your user’s crontab using the following command:

```bash
$ crontab -e
```

This command is used to remove the existing crontab directory.

```bash
$ crontab -r
```

Crontab -l command help you to show the crontab information.

```bash
$ crontab -l
```

It is used to modify the crontab files of other users. This alternative necessitates the use of system administrative access.

```bash
$ crontab -l
```

This directive is used to eliminate your existing crontab directory, prompting you before doing so.

```bash
$ crontab -i
```
