Skip to content
$ lynuxpro
← Linux

LESSON 8 OF 26 · INTERMEDIATE · 17 MIN

Processes and signals

In one minute

  • A process is a program that is running right now. A signal is a short message you send to a process, such as "please stop".
  • When a server is slow, a port is "already in use" or a deploy hangs, you are really asking about processes: what is running, who started it, how much it uses, and how to stop it safely.
  • Every process gets a number called a PID. You find it with ps or top, then send it a signal with kill. The polite signal lets the program tidy up first. The forceful one stops it at once.
  • 👉 High CPU or memory, a stuck program, "port already in use", or stopping an app before a deploy.

Think of it like: a kitchen. Each cook is a process with a name badge (PID). You can politely ask a cook to finish the dish and leave (SIGTERM), or a security guard can drag them out mid-dish (SIGKILL).

What a process has

PropertyMeaning
PIDProcess ID, a unique number
PPIDThe Parent's PID: which process started it
UserWho it runs as (this decides what it is allowed to touch)
StateRunning, sleeping, stopped or zombie
CPU and memoryHow much it is using
CommandThe program and its arguments

PID 1 is the first process Linux starts (usually systemd). Every other process comes from it, like a family tree.

Listing processes: ps

ps takes a snapshot of the running processes.

👉 "Is my app running?", "Who is using all the memory?"

ps auxps aux | grep nginx
SHELLbashutf-8 · 2L
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMANDroot         812  0.0  0.0  55220  1520 ?        Ss   Nov25   0:00 nginx: master processwww-data     813  0.0  0.1  55900  5400 ?        S    Nov25   0:12 nginx: worker processlynuxpro    2211  1.2  3.4 912000 140000 ?       Sl   09:01   0:45 java -jar /opt/app/app.jar
OUTPUTterminalutf-8 · 4L
ColumnMeaning
%CPU / %MEMShare of CPU and memory
VSZMemory the process has reserved (often big, usually not a worry)
RSSMemory it is really using, in KB. This is the number that matters
STATR running, S sleeping, D waiting on disk, Z zombie, T stopped
TIMETotal CPU time used so far

Remember it: ps aux = "show me all processes, with users, including ones not tied to a terminal (x)".

Useful versions on real servers:

ps -ef --forest                     # show the parent-child treeps aux --sort=-%mem | head -n 6     # top 5 memory userspgrep -a java                       # find processes by name
OUTPUTterminalutf-8 · 3L

Live view: top and htop

top is a live, updating view of processes, like Task Manager on Windows.

👉 Something is slow right now and you want to watch what is busy.

KeyWhat it does
PSort by CPU
MSort by memory
kKill a process (asks for the PID)
1Show each CPU core
qQuit

The top lines show the load average (explained in the performance lesson), memory and swap. htop is a friendlier, colourful version, if it is installed.

Stopping processes: signals and kill

Despite its name, kill does not always kill. It sends a signal. For most signals, the process decides how to react.

Stopping a program the polite way lets it finish requests, save data and close files.

SignalNumberMeaningCan the process ignore it?
SIGTERM15"Please shut down cleanly" (the default)Yes, it can tidy up first
SIGKILL9"Stop now", forced by Linux itselfNo
SIGHUP1Often "reload your config"Yes
SIGINT2What Ctrl+C sendsYes
SIGSTOP / SIGCONT19 / 18Pause / resumeNo / yes
kill 2211
SHELLbashutf-8 · 1L
kill 2211          # polite: SIGTERM, lets the app finish requests and close fileskill -9 2211       # last resort: SIGKILL, no cleanup at allpkill -f app.jar   # signal by name instead of PIDkill -HUP 812      # ask nginx to reload its config
OUTPUTterminalutf-8 · 4L

Always try SIGTERM first and wait a few seconds. kill -9 skips all cleanup: temporary files stay behind, connections drop mid-request, and databases may need repair.

Remember it: 15 = please, 9 = now. Ask nicely before you force.

Same rule in Kubernetes: when it stops a pod, it sends SIGTERM, waits (30 seconds by default), then sends SIGKILL. Apps that handle SIGTERM properly can be updated with zero downtime.

Foreground, background and jobs

A foreground program holds your terminal until it finishes. A background program runs while you keep typing.

👉 Running a long task while you do something else in the same terminal.

CommandWhat it does
long-task &Start it in the background
Ctrl+ZPause the program in front
bg / fgResume it in the background / bring it back to the front
jobsList jobs in this terminal
nohup cmd &Keep it running after you log out

For anything that must really keep running, such as an app, use a systemd service (next lesson), not nohup.

Which process is using this port?

👉 You start an app and get "address already in use".

ss -tulpn
SHELLbashutf-8 · 1L
Netid State  Local Address:Port  Processtcp   LISTEN 0.0.0.0:22          sshdtcp   LISTEN 0.0.0.0:80          nginxtcp   LISTEN 127.0.0.1:8080      java
OUTPUTterminalutf-8 · 4L

Now you know Java holds port 8080. On servers, sudo lsof -i :8080 gives the same answer.

Zombies and orphans

  • A zombie (Z) has finished, but its parent has not collected its exit result yet. It uses no CPU or memory, only a PID slot. Many zombies mean the parent program is buggy. Fix or restart the parent.
  • An orphan is a process whose parent died. PID 1 adopts it, so it keeps running normally.

Real-world scenario: a runaway process

CPU is at 100% and the app is slow. You find the cause and stop it politely:

ps auxtop
SHELLbashutf-8 · 2L
ps aux --sort=-%cpu | head -n 3kill 31337sleep 10; ps -p 31337 || echo "stopped cleanly"kill -9 31337        # only if it is still there
OUTPUTterminalutf-8 · 4L
  1. Find the biggest CPU user.
  2. Ask it to stop (SIGTERM).
  3. Wait, then check whether it is gone.
  4. Force it only if it is still there.

Then find out why it ran away (logs, a recent deploy), so it does not happen again.

Common mistakes

  • Using kill -9 first. It skips cleanup and can damage data.
  • Killing the wrong PID. Always check first with ps -p PID -o pid,user,cmd.
  • Running services with nohup. They do not restart after a crash or reboot. Use systemd.

Practice

  1. List all processes and find the nginx master and worker processes.
  2. Find which process listens on port 8080.
  3. Send SIGTERM to the Java process (PID 2211) in the practice terminal.
  4. Explain to a teammate why Kubernetes sends SIGTERM before SIGKILL.

Cheat sheet

CommandWhat it does
ps auxEvery process with CPU and memory
ps aux --sort=-%cpuBiggest CPU users first
top / htopLive view
kill PIDPolite stop (SIGTERM)
kill -9 PIDForced stop (SIGKILL), last resort
pkill -f nameSend a signal by name
ss -tulpnWhich process listens on which port

Key takeaways

  • A process is a running program, and its PID is its number.
  • ps aux and top show what is running. ss -tulpn shows who uses which port.
  • kill sends a message to a program. It does not always kill it.
  • Ask nicely first (kill, signal 15). Force it (kill -9) only as a last resort.
  • Apps that must keep running belong in systemd.