How to Close Programs Using Python on Mac

What will you learn?

In this tutorial, you will master the art of programmatically closing programs using Python specifically on a Mac system. By leveraging Python’s capabilities, you will automate the process of closing specific programs efficiently.

Introduction to the Problem and Solution

When working on a Mac system, there are scenarios where automating the closure of specific programs becomes essential for better task management or automation purposes. Through Python scripting, we can achieve this seamlessly by utilizing powerful libraries and functions.

To tackle this challenge, we will harness the potential of the os module in Python, enabling us to interact effectively with the operating system. Additionally, we will employ the subprocess module to spawn new processes and manage them efficiently.


import os

def close_program(program_name):
    os.system(f"pkill {program_name}")

# Example: Closing Google Chrome Browser
close_program("Google Chrome")

# Copyright PHD

Note: Replace “Google Chrome” with the desired program name for closure.

Our website:


  • Importing Necessary Modules: The os module is imported to facilitate interaction with the operating system.
  • Defining Function: The close_program function takes a program name as input and utilizes os.system() with the pkill command to terminate the specified program.
  • Example: Demonstrates closing Google Chrome browser by calling close_program(“Google Chrome”).
    How does the script determine which program instance to close?

    The script closes programs based on their names provided as arguments, terminating processes accordingly.

    Is it possible to automate closing multiple programs simultaneously?

    Yes, by invoking the close_program() function multiple times with distinct program names within your script.

    Will this method prompt confirmation before closing a program?

    No confirmation dialogues are presented; programs are forcefully terminated.

    Can I schedule this script at specific times using cron jobs or task scheduler?

    Absolutely! You can automate tasks through cron jobs on Unix-based systems like macOS or Task Scheduler on Windows.

    Are there alternative methods besides using ‘os.system()’ for closing programs?

    Certainly! Options include using subprocess.Popen() or third-party libraries like psutil for advanced process management features.


    In conclusion, mastering Python for automating task closures on Mac systems enhances efficiency and flexibility in managing software applications. Understanding modules such as os and functions like .system() empowers users to tailor scripting solutions according to their unique requirements.

    Leave a Comment