Process Management — Terminal

Every running program is a process with a unique number, its PID. ps aux lists them all (a every user, u full format, x including those with no terminal). On…

On Windows, you close a frozen program from Task Manager. In the terminal, you do it by number. Every running program is called a process, and every process has a unique number, its PID (process ID). It's like a restaurant kitchen: every order being cooked has a ticket number. To cancel an order you give its number, not the dish's name, because two plates of fried rice could be cooking at the same time. And you can ask nicely ("please stop"), or just turn off the stove. In the terminal, the order is a process and its ticket number is the PID. Asking nicely and turning off the stove are two ways to stop a process, covered in the kill section.

Looking at processes: ps aux

ps aux lists processes. On a real server the list is long, so this is only a part of it:

$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.1 167740 11904 ?        Ss   08:00   0:02 /sbin/init
www-data     812  2.5  1.2  55320 24160 ?        S    08:01   0:31 nginx: worker process
budi        4188  5.0  3.0 987364 98304 pts/1    Sl+  09:02   0:41 node server.js
budi        4230 97.8  1.0 240112 32768 pts/2    R+   09:05   2:14 python3 worker.py
budi        4301  0.0  0.0  11496  4608 pts/0    R+   09:15   0:00 ps aux

To find one program, you usually send the output of ps aux through a pipe into grep:

$ ps aux | grep node
budi        4188  5.0  3.0 987364 98304 pts/1    Sl+  09:02   0:41 node server.js
budi        4302  0.0  0.0   6612  2304 pts/0    S+   09:15   0:00 grep --color=auto node

The second line is the grep process itself, because its command contains the word node. On Linux, pgrep -a node is cleaner: it only prints 4188 node server.js. On macOS, -a means something else, so use pgrep -lf node. To keep watching processes, use top or htop (press q to quit).

kill: sending a signal

A signal is a short message sent to a process. The command that sends one is called kill, but not every signal kills a process. A process can catch some signals: it runs its own code when the signal arrives, for example to tidy up first.

Signal Number Sent by Can the process catch it?
SIGTERM 15 kill PID (the default) yes: the process can tidy up first
SIGKILL 9 kill -9 PID no: the system stops it right away
SIGINT 2 Ctrl+C yes
SIGTSTP 20 on Linux, 18 on macOS Ctrl+Z yes: the process is paused
SIGHUP 1 closing the terminal yes

In this example, node server.js (PID 4188) and python3 worker.py (PID 4230) both get a plain kill, without -9. Guess first: will both of them surely stop?

$ kill 4188
$ kill 4230
$ pgrep -a python3
4230 python3 worker.py
$ kill -9 4230
$ kill 812
bash: kill: (812) - Operation not permitted
$ kill 4230
bash: kill: (4230) - No such process

kill -15, kill -TERM and kill -SIGTERM are the same thing. So are kill -9, kill -KILL and kill -SIGKILL.

Foreground and background

A process in the foreground (in front) holds your terminal: you can't type another command until that process is done. An & at the end of a command runs it in the background (behind). In this example, node server.js runs a server on port 3000. A port is a network door number where a server receives requests:

$ node server.js &
[1] 4410
Server running on port 3000
$ jobs
[1]+  Running                 node server.js &
$ fg %1
node server.js
^C

Now the server runs in front, and then Ctrl+Z is pressed:

$ node server.js
Server running on port 3000
^Z
[1]+  Stopped                 node server.js
$ bg %1
[1]+ node server.js &

Reading ps with JavaScript

In the exercise, you write the function parseProcessList. It turns output shaped like ps aux, with five columns USER PID %CPU %MEM COMMAND, into an array (a list) of objects, one object per line. One way to write it:

function parseProcessList(psOutput) {
  return psOutput.split("\n").map((line) => {
    const parts = line.trim().split(/\s+/); // /\s+/ = one or more spaces
    return {
      user: parts[0],
      pid: Number(parts[1]),
      cpu: Number(parts[2]),
      mem: Number(parts[3]),
      command: parts.slice(4).join(" "), // the rest, because a command can have spaces
    };
  });
}

console.log(parseProcessList("www 123 2.5 1.2 nginx: worker process"));
// [{ user: "www", pid: 123, cpu: 2.5, mem: 1.2, command: "nginx: worker process" }]

For the example line above, parts holds these pieces:

Index parts[i] Becomes
0 "www" user: "www"
1 "123" pid: 123, a number
2 "2.5" cpu: 2.5
3 "1.2" mem: 1.2
4 to 6 "nginx:", "worker", "process" command: "nginx: worker process"

⚠️ Wrong moves when stopping a process

TL;DR: Every running program is a process with a unique number, its PID. ps aux lists them all (a every user, u full format, x including those with no terminal). On Linux, pgrep -a name finds a program's PID, and the result doesn't include the pgrep process itself (on macOS: pgrep -lf name). top or htop watch processes live. kill PID sends SIGTERM (15): a signal (a message to a process) that asks it to stop and can be caught, so the process can tidy up first or ignore it. kill -9 PID sends SIGKILL: a forced stop with no tidying up, a last resort. Ctrl+C = SIGINT, Ctrl+Z = SIGTSTP (paused, not stopped). & runs a command in the background (the terminal stays free), and jobs, fg %1 and bg %1 manage jobs (processes started from that terminal). In the exercise (turning ps output into objects), use split(/\s+/), Number(...) for pid, cpu and mem, and slice(4).join(" ") for the command.

⚡ Every process has a number

Write:

ps aux

Meaning: Every running program is a process with a unique number, its PID (process ID). ps aux lists them all, with the owner, CPU and memory use, and the command.

⚡ Find one program

Guess first: ps aux | grep node prints one line, but node isn't running. How?

That line is the grep process itself, because its command contains the word node. On Linux, pgrep -a node doesn't find itself.

Meaning: To find one program's PID, use pgrep -a name on Linux, or pgrep -lf name on macOS.

⚡ Ask it to stop first

Write:

kill 4188

Meaning: kill sends the signal (a short message) SIGTERM to the process with PID 4188, meaning "please stop". The process can catch it and tidy up first, or even ignore it. kill -9 forces it to stop with no chance to tidy up, so use it as a last resort.

⚡ Ctrl+Z doesn't stop it

Meaning: Ctrl+C stops the process in front (the one holding the terminal). Ctrl+Z only pauses it. The process still exists, so a paused server still holds its port (the network door number a server uses). fg %1 brings it back to the front (%1 is its job number: the number of that process in this terminal). bg %1 lets it continue in the background, while the terminal stays free.

What you will learn