CodingNic

Environment, Process and Finding

Processes

Environment, Process and Finding 12 min read

Processes

Objectives

By the end of this chapter, you should be able to:

  • Define what a process is
  • List running processes with ps
  • Stop a process with kill

๐Ÿ’ก Why this matters: Every frozen app, runaway script, or server you start from Terminal is a process you may eventually need to find and stop by hand. This is what keeps you from having to force-restart your whole machine over one stuck program.

What Is a Process?

A process is a program actively running on your machine. Your browser is a process. Your email client is a process. Even this terminal window, and the ls command each time you run it, are processes. The operating system keeps each process’s memory walled off from every other one โ€” if one crashes, it shouldn’t take anything else down with it.

Two questions remain: how do you see what’s running, and how do you stop something from the terminal?

Listing Processes with ps

ps on its own only shows processes tied to your current terminal. ps aux is more useful โ€” it shows everything:

  • a โ€” every user’s processes, not just yours
  • u โ€” include who owns each process
  • x โ€” include processes not attached to any terminal

Run ps aux and you’ll get something like:

text
USER  PID    %CPU %MEM VSZ      RSS     TT  STAT STARTED  TIME     COMMAND
sam   74874  8.3  1.7  3408348  139912  ??  S    3:34PM   6:39.74  /Applications/Google Chrome.app/...
sam   74858  6.2  2.8  3224740  233060  ??  S    3:34PM   8:34.62  /Applications/Google Chrome.app/...
sam   61431  2.5  0.6  2698840  53728   ??  R    9:46AM   0:59.49  /Applications/Utilities/Terminal.app/...
Column Meaning
USER Who owns the process
PID A unique number identifying the process
COMMAND What’s actually running

PID is the one you’ll need next.

Stopping a Process with kill

Sometimes a process stops responding and you need to force it to quit. Try this out: open any file with less โ€” less sits there running until you press q. Leave that window open, and in a second terminal window, run:

bash
ps aux

Scroll until you find the less process owned by your username, and note its PID. If the line looks like:

text
sam   10011  0.0  0.0  2434948  900  s000  S+  5:32PM  0:00.00  less readme.md

then the PID is 10011, and killing it looks like:

bash
kill 10011

Switch back to the first window โ€” it should have dropped out of less back to a normal prompt.

kill vs. kill -9

kill works by sending a signal asking the process to terminate โ€” by default, the TERM signal, which a well-behaved program picks up and responds to by shutting down cleanly. A crashed or frozen program sometimes doesn’t respond to that request at all. kill -9 sends KILL instead โ€” described in kill’s manual as “non-catchable, non-ignorable.” If a plain kill doesn’t do anything, kill -9 almost always will.

Try It

Processes are live system state, not something a sandbox can fake โ€” try these yourself:

  1. Open a file with less in one window, find its PID with ps aux in another, and kill it.
  2. Run ps aux and identify three different processes currently running on your machine.
  3. Start a process that ignores a plain kill (a frozen or unresponsive one is rare to find on purpose, so just note: if kill <PID> doesn’t work, what would you try next?).

Recap

  • A process is a running program; ps aux lists every process on the machine, including its PID.
  • kill <PID> asks a process to stop; kill -9 <PID> forces it, for processes that ignore the polite request.

Next lesson: finding files, folders, and text without ever opening a GUI search tool.