How To Set Up Cronjobs on MacOS
Tags: macos, corn, cronjob, crontab
Published: 2021-01-01 23:12:09

---
title: How To Set Up Cronjobs on MacOS
published: true
tags: macos, corn, cronjob, crontab
canonical_url: https://kelvinmwinuka.com/how-to-set-up-cronjobs-on-macos/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-set-up-cronjobs-on-macos
---

<!-- wp:paragraph -->
<p>Cron is a software utility that allows us to schedule tasks (cronjobs) to run periodically, at fixed times or at pre-determined intervals.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>These tasks can serve any desired function. Some common applications include periodically backing up user data or calling an API endpoint.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this article, I'm going to cover the basics of setting up and running cronjobs. Although I use a macOS (Mojave) system, the examples should work on Linux systems, provided all the necessary dependencies are installed.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you are on a linux system, this article will assume you already have all the dependencies installed.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Displaying cronjobs</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In order to display current crobjobs, open the terminal and enter the following command:</p>
<!-- /wp:paragraph -->

```
crontab -l
```

<!-- wp:paragraph -->
<p>If you've never done this before, the output should be empty. Let's change this by actually creating a shell script that will contain the job to be executed periodically.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Creating cronjobs</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now let's create a cronjob. To keep this simple, we will create a simple bash script that prints "Hello, world!" to a file. Create a bash script in the directory of your choice with the following code:</p>
<!-- /wp:paragraph -->

```bash
#!/bin/bash

echo "Hello, world" >> ~/Desktop/job.txt
```

Save this script as 'job.sh' and open a terminal window in the script's directory. Run the script by with the following command:

```
bash job.sh
```

<!-- wp:paragraph -->
<p>A new file named 'job.txt' should have appeared in the directory with the expected output. Run the file several times and you should notice the script append to the file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Running this manually every single time doesn't seem practical. So let's use cron to make this process run automatically at a certain intervals.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Executing cronjob</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we can get cron to execute this bash script in the background, we first have to make sure the script is executable. We can achieve this with the following command:</p>
<!-- /wp:paragraph -->

```
chmod +x job.sh
```

<!-- wp:paragraph -->
<p>Make sure you run this command in the same directory as the script. If not, make sure you specify the path of the script.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, we can add the job to the crontab. Using the following command:</p>
<!-- /wp:paragraph -->

```
crontab -e
```

This gets us into crontab's edit mode. If your default editor is Vi, press 'i' in order to enter insert mode. Now type the following line:

```
* * * * * ~/Desktop/job.sh
```

<!-- wp:paragraph -->
<p>The second part of this command "~/Desktop/job.sh" is the path to the shell script we'd like to execute. In my case, this resides in the desktop folder. Be sure to specify the exact path your script is in.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Notice the "* * * * *" at the beginning of the statement. This is the actual schedule condition for the job's execution.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Each position, from left to right, represents minutes, hours, day of the month, month and day of the week respectively. An asterisk(*) simply means "every". So, in this case, we are executing this job every minute of every hour of every day of the month, every month and every day of the week.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here's an excellent article with and in-depth explanation on <a href="https://www.tweaking4all.com/os-tips-and-tricks/macosx-tips-and-tricks/edit-crontab/" target="_blank" rel="noreferrer noopener">formatting crontab commands</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For now, let's save this by pressing the escape key and then typing ":wq" in order to save and quit.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You should notice the file getting appended to every minute now. Great the cronjob works!</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This was by no means an in-depth look at cronjobs and their power. However, I do think it serves as a sufficient starting guide on setting them up. In a future article, I will demonstrate how to execute python scripts while keeping track of terminal output.</p>
<!-- /wp:paragraph -->

The post [How To Set Up Cronjobs on MacOS](https://kelvinmwinuka.com/how-to-set-up-cronjobs-on-macos/) appeared first on [Kelvin Mwinuka](https://kelvinmwinuka.com).

If you enjoyed this article, consider following my [website](https://kelvinmwinuka.com) for early access to my content before it gets published here (don’t worry, it’s still free with no annoying pop-up ads!). Also, feel free to comment on this post. I’d love to hear your thoughts!