Using lsof


            

lsof command can show which processes are using which resources. This is extremely useful for troubleshooting. Basic syntax is:

lsof [options] [file]

Without arguments, it lists all open files on the system (requires root privileges).

Common Use Cases and Examples:

Find which process is using a file:

<strong>tom@rpi-i2s:~ $</strong> sudo lsof /dev/serial0
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
screen 795 root 6u CHR 204,64 0t0 133 /dev/ttyAMA0

Find which process is using a port:

<strong>tom@rpi-i2s:~ $</strong> sudo lsof -i :80

The above command Lists processes using TCP/UDP port 80 (e.g., web server).

List all open files by a specific user:

<strong>tom@rpi-i2s:~ $</strong> lsof -u pi

The command above shows all files opened by user pi.

List all network connections:

<strong>tom@rpi-i2s:~ $</strong> sudo lsof -i

The command above displays all open network sockets and connections.

List all open files for a process:

<strong>tom@rpi-i2s:~ $</strong> lsof -p 1234

The command above shows all files opened by the process with PID 1234.

Find which process is using a mount point or device:

<strong>tom@rpi-i2s:~ $</strong> sudo lsof /dev/sda1

The command above is useful when you get a “device is busy” error during unmount.

Option Description
-p <pid> List files opened by a specific process ID
-u <user> Show files opened by a user
-i Show network connections
+D <dir> Recursively list all files opened under a directory
-t Output only PIDs (useful in scripts)
-n Skip DNS resolution (faster output)
-P Show port numbers instead of names

Practical Example on Raspberry Pi

If you find that the UART (/dev/ttyAMA0 or /dev/serial0) is busy or inaccessible, run:

sudo lsof /dev/serial0

You might see:

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
agetty    1014 root    3u   CHR 204,64      0t0  1234 /dev/ttyAMA0

This means the serial console service (agetty) is occupying the UART — so your program can’t use it until you stop that service.

In Summary

Task Command Purpose
Check which process uses a port sudo lsof -i :22 Find SSH or HTTP process
Check which process uses a file/device lsof /dev/ttyAMA0 Check serial port usage
See all network connections sudo lsof -i Network diagnostics
Kill a process holding a file kill $(sudo lsof -t /path/to/file) Release a locked file

kill user:

fuser -k /dev/ttyUSB9

That’s it !

Thanks for reading

73

Alternative to ssh-copy-id on windows

Lately I am doing a lot of development on Raspberry Pi systems and I use Visual Studio Code Remote Development. This is a neat feature because it allows me to connect from the same platform to multiple systems at once and work in parallel if I need. Visual Studio Code includes a full featured integrated […]

Attempt to read property “slug” on null

While developing my website I frequently got warnings. When WordPress tries to access the property slug of an object that doesn’t exist (is null), it throws the following (or similar) warning: This usually happens when a menu, a snippet or other piece of code references a category that no longer exist. This means somewhere in […]

Comments are closed.