
When it comes to repetitive tasks, you’re better of automating them. This article will teach you how.
After reading, you’ll know how to automate the execution of two Python scripts for fetching, parsing, and saving data from the web. Let’s get started!
The article is structured as follows:
- What is Cron?
- Writing the Scripts
- Editing Crontab File
- Testing
- MacOS Gotchas
- Conclusion
What is Cron?
Think of Cron as one of the easiest ways to schedule tasks in Linux and macOS environments. The word "Cron" comes from the Greek word "Chronos" (time), and the word "Crontab" stands for "Cron table" or time table. You’ll learn in a bit what this table refers to.
You should use Cron any time you want to automate something, like an OS job or a Python script. Needless to say, but an automated Python script can do basically anything.
On Linux and macOS, the Crontab consists of six fields. The first five are reserved for the date and time of scheduled execution (minute, day of month, month of year, day of week), and the last field is reserved for a command to be executed.
You’re free to use asterisks, values, and ranges for the date-time fields, but more on that in a bit.
You now know what Cron and Crontab are, but we’ll still need some Python scripts before working on scheduling. Let’s cover them next.
Writing the Scripts
Today we’ll schedule two simple scripts. Their job is to fetch the data from the following dummy websites:
These sites contain dummy data on various subjects, but we’ll focus only on the users and posts. The only reason we’re handling two sites is to show how to schedule cron jobs at different time intervals.
Both scripts will fetch the data via requests.get()
API call, parse the JSON data, process it (keep only certain attributes), convert it to a Pandas DataFrame, and save it to a CSV file. Sounds like a lot, but it’s only a few lines of code.
Let’s start with the get_users.py
file:
The get_posts.py
file will be more or less identical – except for the processing part:
Finally, make sure to create the output
just where your scripts are. That’s all we need. If you were to run the scripts, the CSVs would save in the output
directory with the <name>_<timeastamp>.csv
name structure. The timestamps are here to ensure files don’t get overridden.
Let’s see how to schedule the tasks next.
Editing the Crontab File
It doesn’t matter if you’re on Linux or macOS – this section will be identical. There are some permission gotchas for macOS, but we’ll get to that later.
As discussed before, you must follow a specific syntax to schedule cron jobs. The good news is, you can use Crontab.guru website to work on your scheduling.
We want get_users.py
to run on every even minute (e.g., 0, 2, 4) and get_posts.py
to run on every odd minute (e.g., 1, 3, 5). Here’s the correct pattern to run a job on every even minute:

And here’s for the odd minutes:

Neat – let’s use these patterns to schedule executions. Open up a Terminal window and execute pwd
and which python3
commands to get the absolute paths to your script folder and Python:

Once you have these, enter the crontab -e
command to edit a cron file, or to make one if it doesn’t exist:

It will open a VIM editor – from there, click on the I
key on your keyboard to enter insertion mode. You’ll have to specify the scheduling pattern, full path to the Python executable, and full path to the script to make scheduling work. Use the following image as a reference:

Once done, press the ESC
key to exit the insert mode, immediately followed by :wq
and ENTER
keys. This will save the crontab files, and you’ll be back in the Terminal window instantly:

To verify the file was successfully saved, you can use the crontab -l
command – it will list all scheduled jobs:

And that’s all you have to do. Let’s check if scheduling works in the next section.
Testing
There’s nothing left for you to do but to sit and watch your scripts executed every minute. Here’s how my output
folder looks like after a couple of minutes:

As you can see, everything works as expected. Don’t forget to delete these two jobs, or you’ll end up with 1440 new CSV files per day.
MacOS Gotchas
Linux users shouldn’t have any issues, but the story is different on macOS. By default, macOS doesn’t give full disc access to Terminal and Cron, so you’ll have to do that manually.
Just to be clear – you won’t get any errors, but the output
folder will remain empty.
If that’s the case, please follow this article – it will show you how to grant full disc access to both Terminal and Cron.
Conclusion
And there you have it – how to easily schedule Python scripts with Cron on Linux and macOS.
The possibilities are endless – from scheduled web scraping to automated execution of ETL pipelines. The code can change, but the principle remains identical.
Have fun!
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.
Learn more
- 3 Programming Books Every Data Scientist Must Read
- How to Make Python Statically Typed – The Essential Guide
- Object Orientated Programming with Python – Everything You Need to Know
- Python Dictionaries: Everything You Need to Know
- Introducing f-Strings – The Best Options for String Formatting in Python
Stay connected
- Follow me on Medium for more stories like this
- Sign up for my newsletter
- Connect on LinkedIn
- Check out my website