Heading image

Today’s post will demonstrate how to have a script automatically run as a cron job on your Mac computer using crontab.

We will demonstrate by automating a simple script to add the word “hello” onto a new line every minute and showing how we can log out stdout and stderr to a file after each run for our sake.

Getting started

We will create a .scripts folder at the root using to store our scripts to be used by crontab and create a file hello.sh to store our script:

Inside of the file hello.sh, add the following:

The above does the following:

Will all this out of the way, we can now tell crontab to run the script each minute.

Setting the script to run in crontab

On the Mac, we can use crontab -e to open an editor in Vim which will allow us to enter the following.

Note: if you want to use another editor you are familiar with, you will need to update your run command file to change the editor ie if you use Bash, add export EDITOR=/usr/bin/nano to your .bashrc file (or equivalent for your shell of choice). More on this below in "handling issues".

The line added can be broken down into two parts: The cron schedule expression and the command to invoke.

The cron schedule expression in the line above consists of the * * * * * part of the line. This line in layperson's terms means "at every minute". This means that at every minute, the [command] part of the expression will be run.

For those unfamiliar with cron expressions, websites such as crontab guru are great for giving a layperson expression in understandable English.

Below are some basic expressions and some human-readable translations to give you some insight:

As for the rest of the line cd ~/.scripts && ./hello.sh >/tmp/stdout.log 2>/tmp/stderr.log, we simply say "change to the ~/.scripts directory, run ./hello.sh and log stdout to /tmp/stdout.log and stderr to /tmp/stderr.log.

You can update that line to be whatever you want. What is important is that ~/.scripts/hello.sh has the correct permissions to execute.

The script in action

Once you set the value in the crontab file, exit your editor of choice after saving the file.

If successful, you should get an indicator that things are okay if you run crontab -l and see your job listed. If not, check Handling issues below to see if one of them ends up as your issue. If not, you may have to do some online searching!

Assuming that you are successful, all we really need to do now is wait a few minutes and we can cat the ~/.scripts/hello.sh file out and see the results!

Success! We have successfully set up our first automated crontab job in OSX!

To remove the cron job, simple run crontab -e again and remove the job with the editor.

Handling issues

This is not an exhaustive list, but two issues I came across was the need to update my editor to reference /usr/bin/vim instead of /usr/bin/vi and to update my Security & Privacy to allow Full Disk Access for iTerm (enable at your own risk).

To update the editor, ensure that your shell run command files that are loaded (ie ~/.bashrc etc) includes the following export value for EDITOR:

As for the Full Disk Access, simply head to Security and Privacy, enable changes through the lock on the bottom-left and add your terminal of choice.

Security and privacy

Conclusion

The post today is a very beginner-friendly one that does the bare minimum to demonstrate how to get crontab working.

From here, it is up to you to automate what you wish to automate! You can adjust the shell script to run very complex web scrapers in other languages and more!

I personally use it to automate some of my morning preparation with some JavaScript and Python scripts that do some of the morning’s heavy lifting and information aggregation for things that I may need to kickstart my morning off on the right foot.

Resources and further reading

Image credit: Elia Pellegrini

Originally posted on my blog.

Senior Engineer @ Culture Amp. Tinkerer and professional self-isolator in 2020.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store