Sub topic 4
End stuck processes from Terminal
This chapter assumes you can open Terminal, you know how to force quit an app from the Apple menu, and you have already looked at a process in Activity Monitor. Terminal is the next step when the graphical tools are unavailable or when the same app hangs repeatedly.
Why use Terminal
Force Quit and Activity Monitor both need a working desktop. If the Finder is stuck, the menus are slow, or you are connected to the Mac over SSH, Terminal still works. Every app is also a process, which is a running program with its own number, called a process ID (PID). Terminal commands let you find that number and send the process a signal.
Signals: ask first, then force
A signal is a short message the system sends to a process. Two matter here:
| Signal | Command | What it does |
|---|---|---|
| SIGTERM (15) | `kill PID` | Asks the process to quit. It can save work and clean up. |
| SIGKILL (9) | `kill -9 PID` | The system ends the process at once. The app cannot save anything. |
Always try SIGTERM first. Use SIGKILL only if the process ignores the request. This follows the least-disruptive-first order from earlier in the tutorial.
Find the PID and end the process
The command pgrep -x prints the PID of a process whose name matches exactly. The sequence below finds Safari, asks it to quit, waits, and then forces it. Replace 1234 with the number that pgrep prints.
pgrep -x Safari
kill 1234
sleep 5
kill -9 1234If the first kill works, the last command prints kill: 1234: No such process. That message is harmless. It means the process had already quit.
You can only end your own processes with a plain kill. Processes owned by the system or another user need sudo. Be careful with those: ending a system process can log you out or restart the Mac.
Restart the Finder or Dock with killall
killall ends every process with a given name, so you do not need a PID. macOS restarts the Finder and the Dock automatically, which makes this a safe fix for a broken desktop or Dock.
killall FinderYour windows disappear for a moment and then return. Nothing is lost. Do not use killall on a document app such as Pages or Xcode. It sends SIGTERM to every instance of that app, and unsaved changes in the ones that do not respond could be lost.
Common errors
- Name does not match.
pgrep -x Safariprints nothing if the process name differs. Runpgrep -il safarito search by part of the name, ignoring case. - Several matches. If
pgrepprints more than one number, check each one withps -p 1234before you choose. - Operation not permitted. The process belongs to another user or to the system. Do not reach for
sudountil you know what the process is. - The process comes back. Some background processes are restarted by
launchd, the macOS service manager. That is normal for the Finder and the Dock. If an app keeps returning after you end it, check its Login Items in System Settings. - A frozen process will not end even with
kill -9. It is probably stuck waiting on hardware or a network drive. Disconnect the drive. If that does not help, restart the Mac.
Exercise
A frozen app has the PID 5821. Write the commands you would run, in order, and say why you do not start with kill -9.
Show answer
Run kill 5821, wait a few seconds, check that the process is gone with ps -p 5821, and run kill -9 5821 only if it is still there. Plain kill sends SIGTERM, which lets the app save and clean up. kill -9 sends SIGKILL, which ends the process at once and can lose unsaved work.
Exercise
The Dock is showing the wrong icons and does not respond to clicks. Which single command is the least disruptive fix, and why is it safe?
Show answer
killall Dock. macOS relaunches the Dock automatically, so only the Dock restarts and no documents or other apps are affected.