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
auxis three options (flags written without a-).ameans every user's processes,uthe full format with a USER column, andxmeans processes that aren't connected to a terminal are shown too. The TTY column names the terminal a process runs in, and?means no terminal. With no options,psonly shows the processes in your terminal.- USER is the owner, PID the number, %CPU and %MEM the percentage of processor and memory it uses, and COMMAND the command that started it.
- STAT is its state:
Rrunning,Sasleep (waiting for something),Tpaused, and+means the process is in the front (the foreground) of its terminal. worker.pyuses 97.8% CPU: it looks stuck. Andps auxappears in its own list, because at that momentps auxis a running process too.
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 4188sends SIGTERM, meaning "please stop".node server.jscatches it, closes the server cleanly, then exits. When it works,killprints nothing.worker.pygets SIGTERM too. But its handler (the code that runs when the signal arrives) waits for a task that's stuck, soworker.pykeeps running andpgrep -a python3still finds it. SIGTERM is only a request: a process may catch it, even ignore it.kill -9sends SIGKILL. A process can't catch it: the system stops it right away, with no chance to tidy anything up. Use it as a last resort.- You may only send signals to your own processes. PID 812 is nginx (a web server program), which belongs to the user
www-data, sokill 812is refused unless you usesudo. PID 4230 was already stopped bykill -9, so the nextkill 4230answersNo 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
[1]is the job number (a process started from this terminal), and4410is its PID. The terminal is free again right away.jobslists the jobs in this terminal, andfg %1brings job 1 to the front again.- Ctrl+C sends SIGINT to the process in front, and that process usually stops.
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 &
- Ctrl+Z sends SIGTSTP: the process is paused, not stopped.
bg %1continues it in the background,fg %1in the front. nohup command &keeps a command running even after its terminal closes, becausenohupmakes it ignore SIGHUP.- Port 3000 still held by a process you forgot to stop?
lsof -i :3000shows that process's PID, then sendkillto that PID.
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" |
split("\n")breaks it into lines, so two lines give two objects.Number("0.0")gives0, andNumber("5.0")gives5: numbers, not text.slice(4).join(" ")joins every piece from index 4 to the end again, because a command can be several words.
⚠️ Wrong moves when stopping a process
- Going straight to
kill -9. The process gets no chance to delete its temporary files, finish writing a file, or close its database connections. Send a plainkillfirst and wait a few seconds. If the process is still there, then use-9. grepfinding itself.ps aux | grep nodealways prints at least one line, even when node isn't running. Usepgrep.- Thinking Ctrl+Z stops it. The process is only paused and still holds port 3000. So starting the server again fails with
Error: listen EADDRINUSE: address already in use :::3000. Bring the process to the front withfg, then press Ctrl+C. - In the exercise, numbers left as text or a cut-off command.
pid,cpuandmemmust be numbers (Number),commandis every remaining column joined with spaces, and each line becomes one object.
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.