Sub topic 4
End stuck processes from Terminal
This chapter assumes you can open Terminal and run a command, and that you have already tried Force Quit and Activity Monitor from the earlier chapters. Terminal is the next step when those tools are unavailable or too slow. This happens when the Dock or menu bar is unresponsive, when you are connected over SSH, or when an app keeps coming back. It also lets you act on many processes at once.
What a process and a signal are
A process is a running instance of a program. Every process has a process ID (PID), a number that macOS assigns when the process starts. The PID changes each time the app launches, so you must look it up each time.
You do not "delete" a process. You send it a signal, a small message that the system delivers to it. Two signals matter for stuck apps:
| Signal | Number | What it does | Can the app ignore it? |
|---|---|---|---|
| SIGTERM | 15 | Asks the process to quit. The app can save work and clean up. | Yes |
| SIGKILL | 9 | Tells the kernel to stop the process immediately. | No |
kill sends SIGTERM by default, despite its name. kill -9 sends SIGKILL. Always try SIGTERM first. SIGKILL gives the app no chance to save documents or remove temporary files, so unsaved work is lost and lock files can be left behind.
Step 1: find the process ID
pgrep searches running processes by name and prints the matching PIDs.
pgrep -x SafariThe -x option requires an exact match on the whole name. Without it, pgrep Safari also matches names that merely contain "Safari", such as helper processes. If nothing is printed, no process has that exact name, and the command exits with status 1.
Two options help when the name is unclear:
pgrep -il safarimatches case-insensitively (-i) and lists (-l) the process name next to each PID.pgrep -fl "Google Chrome"matches against the full command line (-f), which is useful when the name includes a space.
You can also use ps, which lists processes with details. This command shows the five processes using the most CPU:
ps -Aro pid,%cpu,%mem,comm | head -n 6-A selects all processes, -r sorts by CPU use, and -o chooses the columns. head -n 6 keeps the header line plus five rows. This is the Terminal version of sorting the CPU column in Activity Monitor.
If you already found the PID in Activity Monitor, you can skip this step. The PID column is there, and you can add it through the View menu if it is hidden.
Step 2: ask the process to quit
Use the PID you found. The number 1234 below is only an example.
kill 1234If the command succeeds, it prints nothing. If you see kill: 1234: No such process, the process has already exited or you typed the wrong number. If you see Operation not permitted, you do not own the process.
Wait a few seconds. A busy app may need time to respond, especially if it is writing to disk. Check whether it is gone:
pgrep -x SafariIf no PID is printed, the app has quit.
Step 3: force it to stop
If the process is still there after a reasonable wait, send SIGKILL.
kill -9 1234A process in an uninterruptible state can survive even SIGKILL. In ps output its status shows as U. This usually means it is waiting on hardware or a network volume, for example a disconnected file server or a failing external drive. The signal is delivered when the operation finishes or fails. Disconnecting the device or the network share often frees it. If it does not, a restart is the only fix.
Here is the whole sequence as one small script. It looks up the PID, asks politely, waits, and only then forces. Save it as quit-app.sh.
#!/bin/bash
# Usage: ./quit-app.sh Safari
app="$1"
if [ -z "$app" ]; then
echo "Usage: $0 AppName" >&2
exit 1
fi
pids=$(pgrep -x "$app")
if [ -z "$pids" ]; then
echo "No process named $app is running."
exit 0
fi
echo "Asking $app to quit (PIDs: $pids)"
kill $pids
sleep 5
pids=$(pgrep -x "$app")
if [ -n "$pids" ]; then
echo "$app is still running. Forcing it to stop."
kill -9 $pids
fiRun it with bash quit-app.sh Safari. The variable $pids is deliberately unquoted in the kill lines, so that several PIDs are passed as separate arguments.
Use killall to act by name
killall sends a signal to every process with a given name, so you skip the lookup step.
killall FindermacOS relaunches the Finder automatically after it quits, so this is a common way to clear a misbehaving Finder window or desktop. The same works for the Dock (killall Dock) and the menu bar (killall SystemUIServer), because the system restarts these services on its own.
killall takes a signal option like kill does, for example killall -9 Preview. It matches the process name exactly, and it signals every match, including processes in other windows of the same app. Before you run it, confirm that you want all of them to stop.
The table compares the three tools.
| Tool | Selects processes by | Best for |
|---|---|---|
| `kill` | PID | One specific process, or one of several with the same name |
| `killall` | Exact name | All processes of one app, and restarting Finder or Dock |
| `pkill` | Name pattern or other attributes | Partial matches, or a specific user's processes |
pkill uses the same matching rules as pgrep. A useful habit is to run pgrep with your pattern first. It shows what pkill would signal, without ending anything.
Helper processes and apps that come back
Modern apps often run several processes. A browser may have one main process plus separate processes for tabs and plug-ins. If you end only a helper, the main app stays open and may just restart the helper. Searching with pgrep -il shows the whole family. To end the app completely, end the main process or use killall with the main app's name.
An app that returns right after you end it is usually being restarted by a launch service. This happens with background agents, login items and some updaters. Ending the process treats the symptom. To find the cause, check System Settings under General, then Login Items & Extensions. If it is a stuck system service rather than a user app, the next steps in this tutorial, such as safe mode and a test account, are better tools.
When to prefer another method
- Use Force Quit from the Apple menu or Activity Monitor when you can reach them. They show app names, so there is less risk of a wrong PID.
- Use Terminal when the graphical tools hang, when you connect remotely, or when you need to script the action.
- If the whole system is frozen and even Terminal does not respond, Terminal cannot help. Use a restart instead, as covered in later chapters.
Common mistakes
- Using
kill -9first. Start with plainkill, so the app can save its state. - Copying a PID from an old session. PIDs are reused, so a number from yesterday may now belong to a different program. Look it up again.
- Running
pgrep Safariwithout-x, and ending more processes than you meant to. - Using
sudoto get pastOperation not permittedwithout first checking who owns the process. Runps -p 1234 -o user,commto see the owner and name. - Expecting a force-stopped app to reopen cleanly. Some apps show a recovery prompt or rebuild caches on the next launch, which can take a moment.
Exercise
An app named Notes has stopped responding, and you want to end it from Terminal. Which commands do you run, in what order, and why would you avoid starting with kill -9?
Show answer
Run pgrep -x Notes to get the PID. Then run kill PID (SIGTERM) and wait a few seconds. Run pgrep -x Notes again. If a PID is still printed, run kill -9 PID (SIGKILL). You avoid starting with kill -9 because SIGKILL cannot be handled by the app, so it cannot save data or clean up temporary files.
Exercise
You run kill 4321 and get kill: 4321: No such process. Name two likely causes and what you do next.
Show answer
The process already quit (possibly in response to an earlier signal), or the PID was copied wrongly or came from an earlier session. Run pgrep -x AppName again to see whether the app is still running and to get its current PID.
Exercise
Why can killall Finder be a good fix, while killall Safari may lose data?
Show answer
macOS restarts the Finder automatically, and it holds little unsaved user data, so ending it clears a stuck window or desktop. Safari is a user app that may hold unsaved form input or open work. Also, killall signals every process with that name, so it ends them all at once.