About
lsof lists the open files of running processes, including regular files, pipes and network sockets. On a phone it answers questions such as which program is using the camera, or which process holds a port. Run it when a file is busy or a socket will not release.
Install it
pkg install lsof
Run this inside Termux. If the package is already present, pkg will say so and
do nothing.
Commands worth knowing
6 of the invocations you will actually reach for. Tap the copy button to put one on your clipboard.
lsof -i List only network sockets. Each connection shows the process holding it and its state. lsof -i :8022 Narrow the socket list to one port. See who is listening or connected there. lsof /sdcard/Download/report.pdf Show which processes have a particular file open. Explains a file that refuses to delete. lsof -p 4321 List everything a single process has open. A picture of its files and sockets. lsof -u u0_a123 Restrict the listing to one user's processes. Output stays manageable. lsof +D /sdcard/Download Recursively list open files under a directory. Useful for finding what is holding a folder busy. Worked example
Run lsof -i :8022 and this is what you should see.
$ lsof -i :8022
COMMAND PID USER TYPE NODE NAME
sshd 1204 root IPv4 TCP *:8022 (LISTEN)
sshd 1180 root IPv4 TCP localhost:8022->localhost:51234 (ESTABLISHED) Developed by
lsof project .