Process and port usage in Linux#
Use lsof
, fuser
, ss
, pgrep
, pstree
, ps
, htop
, etc. to find process and port usage in Linux.
Finding process by port#
lsof#
lsof -i:8080
lsof -i :8080
$ lsof -i:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
uvicorn 40268 xiang 3u IPv4 1054329 0t0 TCP localhost:8000 (LISTEN)
python3 40273 xiang 3u IPv4 1054329 0t0 TCP localhost:8000 (LISTEN)
# with -t option to only display PID
$ lsof -ti:8000
40268
40273
killing process by lsof#
fuser#
killing process by fuser#
ss#
# find by port
$ ss -lapute | grep 8000
tcp LISTEN 0 2048 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=53146,fd=3),("uvicorn",pid=53141,fd=3)) uid:1002 ino:1271011 sk:1001 cgroup:/ <->
# find by name
00:30 $ ss -lapute | grep uvicorn
tcp LISTEN 0 2048 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=53146,fd=3),("uvicorn",pid=53141,fd=3)) uid:1002 ino:1271011 sk:1001 cgroup:/ <->
$ ss -lapute 'sport = :8000'
# only display PID
$ ss -lapute 'sport = :8000' | sed -r 's/.*pid=([0-9]+).*/\1/'
Finding process by name#
pgrep#
pgrep:
-n, --newest select most recently started
-o, --oldest select least recently started
$ pgrep -f uvicorn
57506
$ pgrep -fa uvicorn
57506 /home/xiang/git/fastapi-demo/.venv/bin/python3 /home/xiang/git/fastapi-demo/.venv/bin/uvicorn app_sqlalchemy_v1.main:app --reload
pstree#
# -n for most recently started, -o for least recently started
$ pstree -p $(pgrep -n uvicorn)
uvicorn(57506)─┬─python3(57507)
├─python3(57508)───{python3}(57509)
└─{uvicorn}(57510)
$ pgrep uvicorn | xargs pstree -p
uvicorn(57506)─┬─python3(57507)
├─python3(57508)───{python3}(57509)
└─{uvicorn}(57510)
$ pstree -ps $(pgrep -n uvicorn)
systemd(1)───init-systemd(Ub(2)───SessionLeader(464)───Relay(466)(465)───sh(466)───sh(467)───sh(472)───node(476)───node(579)───bash(30994)───make(57499)───uvicorn(57506)─┬─python3(57507)
├─python3(57508)───{python3}(57509)
└─{uvicorn}(57510)
$ pstree -pas $(pgrep uvicorn)
systemd,1
└─init-systemd(Ub,2
└─SessionLeader,422
└─Relay(424),423
└─bash,424
└─make,27286 run-sqlalchemy-v1
└─uvicorn,27293 /home/xiang/git/fastapi-demo/.venv/bin/uvicorn app_sqlalchemy_v1.main:app --reload
├─python3,27294 -c from multiprocessing.resource_tracker import main;main(4)
├─python3,27295 -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=5, pipe_handle=7) --multiprocessing-fork
│ └─{python3},27296
└─{uvicorn},27297
ps#
00:37 $ ps -faux | grep uvi -B3
xiang 1799 0.0 0.0 7932 7056 pts/9 Ss+ Feb14 0:00 | | | \_ /bin/bash --init-file /home/xiang/.vscode-server/bin/e54c774e0add60467559eb0d1e229c6452cf8447/out/vs/workbench/contrib/terminal/common/scripts/shellIntegration-bash.sh
xiang 30994 0.0 0.0 7968 7252 pts/10 Ss Feb14 0:00 | | | \_ /bin/bash
xiang 57499 0.0 0.0 3500 2608 pts/10 S+ 00:10 0:00 | | | | \_ make run-sqlalchemy-v1
xiang 57506 45.7 0.5 130272 54744 pts/10 Sl+ 00:10 12:34 | | | | \_ /home/xiang/git/fastapi-demo/.venv/bin/python3 /home/xiang/git/fastapi-demo/.venv/bin/uvicorn app_sqlalchemy_v1.main:app --reload
--
root 69673 0.0 0.0 2784 220 ? S 00:26 0:00 \_ /init
xiang 69681 0.0 0.0 7776 6972 pts/18 Ss 00:26 0:00 \_ -bash
xiang 77067 0.0 0.0 9988 5520 pts/18 R+ 00:37 0:00 \_ ps -faux
xiang 77068 0.0 0.0 4092 2012 pts/18 S+ 00:37 0:00 \_ grep --color=auto uvi -B3
htop#
Enter into htop
, press /
or F3
to search for a process name, or press F4
to filter by the process name, then F9
to kill the process name.
Finding lines in input#
# display lines containing "pattern" in {filename}
grep -n "pattern" {filename}
# grep -E is same as egrep
# Using grep -E with extended regex for alternation:
grep -E "cat|dog" {filename}
# Using grep -e to specify multiple explicit patterns:
grep -e "cat" -e "dog" {filename}