Python

How to schedule a Python script on a Mac

In a previous article, we talked about how to run Python from the Windows Task Scheduler. This post will cover how to schedule Python tasks on a Mac operating system as well as give an overview of the schedule package.

Using crontab on Mac

Python tasks can be scheduled on Mac using crontab. To do that, first, open up the Terminal. Then, we need to modify the crontab file. We can do that by typing crontab -e.

This will open crontab in the default editor, which is typically vim. You can change the editor by adding the editor name in front of our command – for example, to modify the crontab file using nano, we can run nano crontab -e (followed by enter).

Next, we need to add in a line describing the schedule frequency of how often we want the Python script to run. We input this in the order: minute, hour, day of the month, month, and day of the week. To leave one of these unspecified, place an asterisk (*) in that date / time slot.

Example 1: Run script on the first day of each month at 2:03.


3 2 1 * * python /path/to/test_script.py

Example 2: Run script every minute


* * * * * python /path/to/test_script.py

Example 3: Run script every hour at the 30th minute


30 * * * * python /path/to/test_script.py

Once you’ve scheduled your Python script, you need to save the crontab file. If you’re using nano, you can do that by typing ctrl+0 followed by ctrl+x to exit. In vim, you can save and exit by hitting esc and then typing :w, followed by enter.

Handling potential issues

One issue that may come up has to do with permissions with “full disk access”. This may happen if you’re using a recent version of macOS Mojave. For details on how to fix this issue, see here.

Another common issue that occurs is with path names. This is always good to check – for example, if you have multiple installations of Python installed, you’ll want to point to the correct installation needed for your scheduled job.

The schedule package

In addition to using crontab to run Python scripts, we can also use the schedule library for handling scheduling Python tasks. This package is especially useful for scheduling specific functions within Python applications. It also works across different operating systems. Let’s get started with the schedule package by installing it with pip:


pip install schedule

We’ll start by creating a simple function called test that simply prints out a message. Then, we can use schedule to schedule a task running this function every 10 seconds. To kick off this task, we can use the run_pending method, which will run inside of a while loop.


import schedule

def test():
    print("Test this out!")

schedule.every(10).seconds.do(test)

while True:
    schedule.run_pending()

The recurrence of the task can be adjusted by changing “seconds” to “minutes” or “hours” like below.


# every 10 minutes
schedule.every(10).minutes.do(test)

# every 10 hours
schedule.every(10).hours.do(test)

Likewise, we can change the time unit length from 10 to any other number.


# every 1 minute
schedule.every(1).minutes.do(test)

# every 30 seconds
schedule.every(30).seconds.do(test)

Listing and clearing tasks

You can print out a list of the scheduled tasks by running schedule.jobs:


schedule.jobs

To clear out all tasks from the scheduler, we can run the following line of code:


schedule.clear()

Learn more about schedule by clicking here.

Scheduling Python tasks from the Windows Task Scheduler

If you’re running on Windows, you can also schedule tasks via the built-in Task Scheduler. Learn more about how to do that by clicking here.

Conclusion

That covers it for this post. Check out other Python posts here.

Andrew Treadway

Recent Posts

Software Engineering for Data Scientists (New book!)

Very excited to announce the early-access preview (MEAP) of my upcoming book, Software Engineering for…

2 years ago

How to stop long-running code in Python

Ever had long-running code that you don't know when it's going to finish running? If…

3 years ago

Faster alternatives to pandas

Background If you've done any type of data analysis in Python, chances are you've probably…

3 years ago

Automated EDA with Python

In this post, we will investigate the pandas_profiling and sweetviz packages, which can be used…

3 years ago

How to plot XGBoost trees in R

In this post, we're going to cover how to plot XGBoost trees in R. XGBoost…

4 years ago

Python collections tutorial

In this post, we'll discuss the underrated Python collections package, which is part of the…

4 years ago