How To Set Up Cronjobs on MacOS — Kelvin Mwinuka

Cron is a software utility that allows us to schedule tasks (cronjobs) to run periodically, at fixed times or at pre-determined intervals.

These tasks can serve any desired function. Some common applications include periodically backing up user data or calling an API endpoint.

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.

If you are on a linux system, this article will assume you already have all the dependencies installed.

Displaying cronjobs

crontab -l

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.

Creating cronjobs

#!/bin/bashecho "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

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.

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.

Executing cronjob

chmod +x job.sh

Make sure you run this command in the same directory as the script. If not, make sure you specify the path of the script.

Now, we can add the job to the crontab. Using the following command:

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

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.

Notice the “* * * * *” at the beginning of the statement. This is the actual schedule condition for the job’s execution.

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.

Here’s an excellent article with an in-depth explanation on formatting crontab commands.

For now, let’s save this by pressing the escape key and then typing “:wq” in order to save and quit.

You should notice the file getting appended to every minute now. Great the cronjob works!

Conclusion

If you enjoyed this article, consider following my personal website for early access to my content before it gets published on Medium (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!

Originally published at https://kelvinmwinuka.com on October 8, 2020.

Full-stack software developer. #WebDev #Programming