Close Apps on macOS from the Command Line
In this article
You can close apps on macOS from the command line using the osascript command (which sends AppleScript commands) or the kill command. The quickest way is osascript -e 'quit app "AppName"' for a graceful quit, or killall AppName for a forced termination. This guide covers both methods, plus how to target specific processes and handle apps that won't close.
What You'll Need
- Access to the Terminal app (found in /Applications/Utilities/Terminal.app, or search with Spotlight).
- Admin (sudo) privileges only if you need to force-quit a process running under another user; for your own apps, no special rights are needed.
- The exact name of the app or its process name—see Step 1 below.
Step-by-Step
- Find the exact app name or process name. Open Terminal and type
ps -ax | grep -i appname(replaceappnamewith a guess). For example,ps -ax | grep -i safarishows lines containing “Safari”. The process name (likeSafari) appears in the last column. Alternatively, useosascript -e 'tell application "System Events" to get name of every process whose background only is false'to list all visible apps. - Quit an app gracefully with osascript. Use the command:
osascript -e 'quit app "Safari"'. ReplaceSafariwith the app’s display name (as it appears in the menu bar or Finder). This sends a quit Apple event, which is the same as pressing Cmd+Q. The app gets a chance to save documents and close windows cleanly. - Quit multiple apps at once. You can chain commands:
osascript -e 'quit app "Safari"' -e 'quit app "Mail"', or use a list:osascript -e 'tell application "System Events" to quit every process whose name is in {"Safari", "Mail"}'(but note that this may not work for all apps; the first method is more reliable). - Force-quit an app with killall. If an app is unresponsive, use
killall Safari(case-sensitive). This sends a SIGTERM signal, which asks the app to terminate; if it doesn’t respond, you can usekillall -9 Safarito send SIGKILL, which immediately kills the process without saving. Use-9only as a last resort because unsaved data will be lost. - Force-quit a specific process by PID. First find the PID (process ID) with
pgrep -x Safariorps -ax | grep Safari. Then runkill -9 PID(replace PID with the number). This is useful when multiple processes share a similar name. - Quit an app that is not in the Dock. Some background apps (like menu bar utilities) may not have a display name. In that case, use the process name from
psand thekillallmethod. - Use a one-liner to quit all apps except Finder. If you want to close everything, run:
osascript -e 'tell application "System Events" to set visibleApps to name of every process whose visible is true' -e 'repeat with appName in visibleApps' -e 'if appName is not "Finder" then quit application appName' -e 'end repeat'. This is advanced; test with a single app first.
Common Mistakes
- Using the wrong name. The app’s display name (e.g., “System Settings”) may differ from its process name (e.g., “System Settings” is actually “System Settings” but some apps have different names like “Google Chrome” vs. “Google Chrome Helper”). Always check with
ps. - Using
killall -9too quickly. This can corrupt documents or leave temporary files. Trykillallwithout-9first, wait a few seconds, then escalate. - Forgetting that
osascriptrequires the app to support Apple events. Most GUI apps do, but some background processes don’t. If you get an error like “application isn’t running”, usekillall. - Typing the process name with spaces incorrectly. In
killall, enclose the name in quotes if it has spaces:killall "App Name". - Using
killon a PID that has already exited. You’ll get “no such process”. Recheck withps.
Tips
- Create an alias in your shell profile. Add a line like
alias quitapp='osascript -e "quit app \"$1\""'to your~/.zshrc(or~/.bash_profile) to shorten the command. - Use
pkillfor pattern matching.pkill -i safariwill kill any process whose name contains “safari” (case-insensitive). This is handy when you have multiple helper processes. - Check if the app is running first. Use
pgrep -x Safari; if it returns a PID, the app is running. If not, you’ll get no output. - For Finder, you can only restart it, not quit it. Use
killall Finderto restart the Finder (it will relaunch automatically). - Combine with
sleepto give apps time to save. For example:osascript -e 'quit app "Safari"' && sleep 2 && killall Safarito force after a graceful attempt.
Troubleshooting
If the app doesn’t quit with osascript: First, verify the app name is correct by running osascript -e 'tell application "System Events" to get name of every process'. If the app is running but the command returns an error like “Application isn’t running”, it may be a background-only process. Use killall with the process name from ps.
If killall says “No matching processes” but the app is open: The process name may be different from the app name. Run ps -ax | grep -i appname to see the actual process name. For example, “Google Chrome” runs as “Google Chrome”, but some apps have helper processes like “Dropbox” running as “Dropbox” plus “Dropbox Helper”.
If the app still won’t die after killall -9: This is rare. Check if the process is “zombie” (shown as Z in the STAT column of ps). Zombie processes are already dead and will be cleaned up by the system; you can’t kill them. If it’s stuck in an uninterruptible sleep (STAT U), you may need to restart your Mac.
If you get a permission error: You may be trying to kill a process owned by another user (like root). Use sudo killall -9 AppName (you’ll be prompted for your password). Be careful—only do this if you know what you’re doing.
If you accidentally quit the Finder: It will relaunch automatically. If it doesn’t, run open /System/Library/CoreServices/Finder.app to relaunch it.
Frequently Asked Questions
What’s the difference between osascript and killall?
osascript sends a polite “quit” request, allowing the app to save data and close windows. killall sends a termination signal directly to the process; without -9, it’s still a request, but with -9 it’s an immediate kill that doesn’t allow saving.
Can I close an app that isn’t responding?
Yes, use killall -9 AppName or kill -9 PID. This will force-quit the app, but you may lose unsaved changes.
How do I close all apps at once?
You can use the AppleScript one-liner provided in Step 7, or run osascript -e 'tell application "System Events" to quit every process whose background only is false'. Note that this will close all visible apps, including your Terminal, so be prepared.
Why does osascript say “Application isn’t running” even though I see it on screen?
This happens when the app is a background process or doesn’t respond to Apple events. Check the process name with ps and use killall instead.
Can I close an app by its window title?
No, the command line works with process names, not window titles. To close a specific window, you’d need to use AppleScript to target the window, but that’s more complex and not reliable across all apps.
Is it safe to use kill -9 on any app?
It’s safe in the sense that it won’t damage your system, but it can cause data loss for the app. Always try a graceful quit first. Avoid using -9 on system processes like kernel_task or WindowServer—that can crash your Mac.