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 auxEvery process with user, CPU, memory and full command, BSD style; works on Linux and macOS
ps -efEvery process with UID, PID and parent PID in the UNIX style that procps documents
ps -o pid,ppid,%cpu,%mem,comm -p 1Pick the columns you want for one PID; ran on macOS, where PID 1 is launchd
ps -eo pid,rss,comm | sort -k2 -nr | headLargest resident memory users first, sorted with plain sort so it works on macOS too
ps -eo pid,comm,%mem --sort=-%mem | headSame idea using procps' own --sort; Linux only
top -o %MEMStart procps top sorted by memory instead of CPU
top -p 1234Watch a single PID in procps top
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.jsFind processes whose full command line contains srv.js and show PID plus name; ran on macOS
pgrep -a nodeShow matching PIDs with the full command line; -a is procps, macOS uses -fl
pgrep -u postgresMatch only processes owned by one user
pkill -TERM -f srv.jsAsk every process whose command line matches to exit; ran on macOS and the server stopped
kill -lList signal names; on macOS this printed HUP INT QUIT ... USR1 USR2
kill -TERM 1234Politely stop one process; TERM (15) lets it run its shutdown code
kill -KILL 1234Force-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 1234Send a hangup; many daemons document it as reload-config, so check the program's own docs first
-n skips DNS lookups and -P keeps port numbers as numbers, which makes lsof much faster.
lsof -nP -iTCP -sTCP:LISTENList every TCP socket in LISTEN state with the owning command and PID; ran on macOS
lsof -nP -iTCP:4317 -sTCP:LISTENShow which process is listening on one TCP port
lsof -ti :4317Print 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
lsof -p 1234List every file, socket and library one process has open
Not available on macOS; use lsof there. The netstat page is https://man7.org/linux/man-pages/man8/netstat.8.html.
ss -ltnpListening TCP sockets, numeric ports, with the owning process (run as root to see other users' processes)
ss -lunpSame for UDP sockets
ss -t -aAll TCP sockets, listening and connected
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
netstat -tlnpThe older net-tools equivalent of ss -ltnp, if net-tools is installed
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 nodeand killing the PID of the grep itself.Use
pgrep -f node, which never matches its own process.Reaching for
kill -9first, 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 -10for 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=-%memon a Mac and gettingillegal option.macOS ps has no
--sort; pipeps -eo pid,rss,commintosort -k2 -nrinstead.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.
Related cheat sheets
Sources
- https://man7.org/linux/man-pages/man1/ps.1.html
- https://man7.org/linux/man-pages/man1/kill.1.html
- https://man7.org/linux/man-pages/man7/signal.7.html
- https://man7.org/linux/man-pages/man1/pgrep.1.html
- https://man7.org/linux/man-pages/man8/lsof.8.html
- https://man7.org/linux/man-pages/man8/ss.8.html
- https://man7.org/linux/man-pages/man8/netstat.8.html
- https://man7.org/linux/man-pages/man1/top.1.html
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.