Skip to content

Menu

Processes and Ports Cheat Sheet: ps, kill, lsof and ss

Checked against ps, kill, pgrep, lsof, ss man7.org pages for procps-ng, iproute2, lsof and net-tools (upstream as of 4 Aug 2026), signal(7) from man-pages 6.19; local runs on macOS 26 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet covers the two questions that come up every week: what is running, and what is holding this port. It follows the Linux man pages on man7.org and notes where macOS behaves differently. The macOS rows were run on a Mac; Linux-only rows such as ss and procps top options are marked untested.

When to use what

To find a process by name, use pgrep before you reach for ps aux | grep. It skips its own grep line and gives you PIDs you can feed straight to kill. Add -f when the name you know is in the arguments, like a script path, rather than in the program name.

To find what owns a port, use ss -ltnp on Linux and lsof -nP -iTCP -sTCP:LISTEN on macOS. ss comes from iproute2 and is the documented replacement for netstat, whose own man page calls it mostly obsolete. lsof works on both systems but is slower on a busy box. The port reference tells you what a number usually means before you go hunting. If the owner turns out to be ssh, it is probably a forwarded port; SSH tunnels and port forwarding explains how those are set up.

When stopping things, send TERM first and give the program a moment to clean up. KILL cannot be caught, so open files, temp directories and child processes are left as they are. Use signal names, not numbers: USR1 is 10 on x86 Linux and 30 on macOS. If you run services under systemd, stop them with systemctl so they don't come straight back.

procps ps accepts both UNIX (-ef) and BSD (aux) styles. macOS ps also takes aux and -o, but --sort fails there with illegal option. top options differ completely between procps and macOS; see https://man7.org/linux/man-pages/man1/top.1.html.

ps aux

Every process with user, CPU, memory and full command, BSD style; works on Linux and macOS

ps -ef

Every process with UID, PID and parent PID in the UNIX style that procps documents

From the docs, not run by us
ps -o pid,ppid,%cpu,%mem,comm -p 1

Pick the columns you want for one PID; ran on macOS, where PID 1 is launchd

ps -eo pid,rss,comm | sort -k2 -nr | head

Largest resident memory users first, sorted with plain sort so it works on macOS too

ps -eo pid,comm,%mem --sort=-%mem | head

Same idea using procps' own --sort; Linux only

From the docs, not run by us
top -o %MEM

Start procps top sorted by memory instead of CPU

From the docs, not run by us
top -p 1234

Watch a single PID in procps top

From the docs, not run by us

Signal names and numbers are in https://man7.org/linux/man-pages/man7/signal.7.html. SIGKILL and SIGSTOP cannot be caught, blocked or ignored.

pgrep -fl srv.js

Find processes whose full command line contains srv.js and show PID plus name; ran on macOS

pgrep -a node

Show matching PIDs with the full command line; -a is procps, macOS uses -fl

From the docs, not run by us
pgrep -u postgres

Match only processes owned by one user

From the docs, not run by us
pkill -TERM -f srv.js

Ask every process whose command line matches to exit; ran on macOS and the server stopped

kill -l

List signal names; on macOS this printed HUP INT QUIT ... USR1 USR2

kill -TERM 1234

Politely stop one process; TERM (15) lets it run its shutdown code

kill -KILL 1234

Force-stop a process that ignores TERM; it gets no chance to clean up

SIGKILL is 9 on every architecture listed in signal(7).

kill -HUP 1234

Send a hangup; many daemons document it as reload-config, so check the program's own docs first

From the docs, not run by us

-n skips DNS lookups and -P keeps port numbers as numbers, which makes lsof much faster.

lsof -nP -iTCP -sTCP:LISTEN

List every TCP socket in LISTEN state with the owning command and PID; ran on macOS

lsof -nP -iTCP:4317 -sTCP:LISTEN

Show which process is listening on one TCP port

lsof -ti :4317

Print only the PID for anything using port 4317, ready for kill; -t is terse output

kill $(lsof -ti :4317)

Stop whatever holds a port in one line; check the PID first on a shared machine

From the docs, not run by us
lsof -p 1234

List every file, socket and library one process has open

From the docs, not run by us

Not available on macOS; use lsof there. The netstat page is https://man7.org/linux/man-pages/man8/netstat.8.html.

ss -ltnp

Listening TCP sockets, numeric ports, with the owning process (run as root to see other users' processes)

From the docs, not run by us
ss -lunp

Same for UDP sockets

From the docs, not run by us
ss -t -a

All TCP sockets, listening and connected

From the docs, not run by us
ss -o state established '( dport = :ssh or sport = :ssh )'

Established SSH connections in either direction, with timer info; filter syntax from the ss man page

From the docs, not run by us
netstat -tlnp

The older net-tools equivalent of ss -ltnp, if net-tools is installed

From the docs, not run by us

Worked example: Free a port that a dev server left behind

Your app fails with "address already in use" on port 4317. First find the owner. On macOS (this was run locally):

$ lsof -nP -iTCP:4317 -sTCP:LISTEN
COMMAND   PID   USER   FD   TYPE  DEVICE  SIZE/OFF NODE NAME
node    15301 rupraj   12u  IPv6  0x...        0t0  TCP *:4317 (LISTEN)
$ pgrep -fl srv.js
15301 node srv.js

Both commands agree: PID 15301 is a leftover node srv.js. The *:4317 means it listens on every interface, not only localhost. Now ask it to stop and confirm it went:

$ pkill -TERM -f srv.js
$ pgrep -f srv.js || echo gone
gone

On Linux the first step is ss -ltnp 'sport = :4317', where -p adds the process using the socket to each line (not run here). Only escalate to kill -KILL if the process is still listed a few seconds after TERM.

Common mistakes

  • Running ps aux | grep node and killing the PID of the grep itself.

    Use pgrep -f node, which never matches its own process.

  • Reaching for kill -9 first, then finding stale lock files and orphaned child processes.

    Send TERM, wait a few seconds, and only then send KILL to whatever is left.

  • Hard-coding kill -10 for SIGUSR1 in a script that also runs on a Mac.

    Use names like kill -USR1; signal(7) shows the numbers differ by architecture and macOS uses 30.

  • Running ps -eo pid,comm --sort=-%mem on a Mac and getting illegal option.

    macOS ps has no --sort; pipe ps -eo pid,rss,comm into sort -k2 -nr instead.

  • Killing a service process that systemd or a supervisor immediately restarts.

    Stop it through the manager, for example systemctl stop name, so the restart policy does not undo your work.