Cron Job made easy: A setup guide to cron
Tags: linux, beginners, cron, bash
Published: 2020-07-22 09:17:40

You can use a cron job for any program which runs on Linux by using the following method.

<h2><b>Tl;dr</b></h2>

-  Create a `script.sh` file perform any action.
-  Make use of `crontab -e` command to create the cron job.
-  Set the job to run the script for the specified timings.
-  We troubleshoot the job by looking at the logs.


 <h2>Contents</h2>

  - [Commands to manage cron jobs.](#commands)
  - [Cron syntax.](#syntax)
  - [Setting up the script.](#script)
  - [Troubleshoot.](#troubleshoot)
  - [Conclusion](#conclusion)

Cron is a useful tool in Linux that most developers love because it lets you run automated commands at specific periods (minutes, hours, days, etc.).

Cron jobs run in the background and its keep checking few files and directories (`/etc/crontab/`, `/etc/cron.*/` and `var/spool/cron` ..etc). Cron files are not supported to be edit directly, and each user has a unique crontab. The `crontab` stands for the cron table. It is the list of commands that you want to run on a regular schedule.

<h3 id="commands"> Commands </h3>
<p>
The commands for creating and editing cron jobs are the same and simple. And what's even cooler is that you don't need to restart the cron after making changes to the existing one.<br/> cool! right?
</p>
let's view our cron table entries before creating a new one.

```bash
crontab -l 
```
It will list if it has any. Anyway, let's move on to the step to create a cron job.

```bash
crontab -e
```
Nothing special, single parameter change to command!.
The command may prompt you to select a text editor(nano, vim), go on with the comfortable one.

Now, we have a crontab file opened on a text editor to write our entries.
<h3 id="syntax"> Cron syntax </h3>
<p>Just as it is with any language, working with cron is a lot easier when you understand its syntax. </p>

` MIN HOUR DOM MON DOW CMD `

Field | Description | Allowed Value
--- | --- | ---
MIN   |  Minute field | 0 to 59
HOUR  |  Hour field    | 0 to 23
DOM   |  Day of Month  | 1-31
MON   |  Month field   | 1-12
DOW   |  Day Of Week   | 0-6
CMD   |  Command       | Any command to be executed.

That's not all. Cron uses 3 operator symbols which allow you to specify multiple values in a single field.
- Asterisk (*) - specifies all possible values for a field.
- The comma (,) - specifies a list of values.
- Dash (-) - specifies a range of values.
- Separator (/) - specifies a step value.


 By looking at the syntax we can write our entries.

```
0 3 * * * /home/user/path/to/script.sh
```
Meaning, Run `/home/user/path/to/script.sh` at 3 am every day.
You can adjust periods by changing the time parameters.
```
*/30 * * * * /home/user/path/to/script.sh
```
The above one will run the script every 30 minutes.

```
30 * * * * /home/user/path/to/script.sh
```
This would run at 1:30,2:30,3:30.

```
0,30 * * * * /home/user/path/to/script.sh
```
This would run at 1:30,2:00,2:30,3:00,3:30.

Write this entry on the text editor we're previously opened using
 `crontab -e` command. Before saving it we need to set up our script for this.

<h3 id="script"> Setting up the script </h3>

Cron doesn't support `relative path`, therefore you've to write the `absolute path` for everything that you are pointing.

You can find the absolute paths of directories by simply typing `pwd` on your terminal.


Make sure your script has execute permission.

```
chmod +x script.sh
```

**That's it!**

Save it all to run the cron job for the specified time period.

<h3 id="troubleshoot"> **Troubleshoot** </h2>

Cron jobs are commands that your service runs at a specified interval and, as such, can be difficult to troubleshoot.

Although we can't directly troubleshoot, some of the common mistakes are:

1. Using a relative path. You must be sure to use only absolute paths inside that script.
2. Permissions are too strict. Please be sure all scripts, files that are being used are set to executable. 

`chmod +x <file name>`

You can check the cron logs to make sure that the crontab is working correctly. The logs are default located in 
`/var/log/syslog`. And running the following grep command will get you all cron logs.

```bash
grep -i "CRON" /var/log/syslog
```

If you're not finding the syslog file in your system, Then try the below command.

```bash
journalctl | grep "CRON"
```

<h3 id="conclusion"> **Conclusion** </h2>

Let's go through the steps we've done. 

-  Created a `script.sh` file to perform the action.
-  Add an entry to the cron table using `crontab -e` command.
-  The crontab entry should point to the script.sh file.
-  We troubleshoot the job by looking at the logs