lsof: the files a process holds open
Video 129 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:44, with English and Hindi audio.
A disk is full, but the files on it look small. The missing space is often held by a file that was deleted by name while a running program still has it open. The lsof command lists the files, network connections and pipes that each process has open, so you can find that holder. This article shows how to use it with a process, a port, a path and a deleted file.
What you will learn
- An open file is anything a running process has opened, such as a file, a network connection or a pipe.
- A deleted file keeps its space while a program still has it open, so a full disk can have a hidden cause.
- lsof with the p option lists the files one process holds, and lsof with the i option finds who holds a port.
- lsof +L1 lists open files that were deleted but are still held.
- Without sudo, lsof shows only your own files, so the picture is incomplete.
A full disk with no visible cause
On a warehouse server, the data disk is almost full, and new writes start to fail. The engineer looks at the folders, and they add up to much less than the disk says is used. The missing space is held by a file that was deleted by name, but a running program still has it open.
An open file is anything a running process has opened for use. That includes ordinary files, network connections, and pipes between programs. The kernel tracks each one, and the space of a file is freed only when no process holds it any longer.
Finding the files, ports and holders
To see the files one process holds, give lsof its process ID with the p option. Each row shows a file, a socket, or a pipe.
sudo lsof -p 4321 | head -n 6To find who holds a port, use the i option with a colon and the port number. The port 5432 used here is the usual one for PostgreSQL.
sudo lsof -i :5432To find every process that holds one file open, give lsof the file's path. Use the real path of your own file.
sudo lsof /srv/exports/orders.csvDeleted files that still take space
A log file on the data disk can be deleted while an export job still writes into it. The name disappears from the folder, but the job keeps writing into the invisible file, and the disk fills up. The plus L one option finds those files, so run it when a disk is full and the folders look small.
sudo lsof +L1Restarting the program that holds the file closes it. The kernel then frees the space.
Reading the columns
- FD: the file descriptor. Numbers name the files the process opened, and letters such as cwd name special entries.
- TYPE: what the entry is, such as a regular file, a directory, a network socket, or a pipe.
- NAME: the path of the file or the connection. When a file was removed while open, the word deleted appears after the name.
What goes wrong with lsof
- No sudo, partial picture: without sudo, lsof shows only your own files, so other users' open files stay hidden.
- A slow command: a full listing on a busy machine can take a long time, so give lsof a process, a port or a path first.
Try it yourself
You need a Linux server where you have sudo rights. The steps only read the open files, and change nothing. Use a process ID and a file path of your own in place of the examples.
List the first open files of process 4321. Use a process ID of your own in its place.
sudo lsof -p 4321 | head -n 6What you should see: Each row shows a file, a socket or a pipe that the process holds open.
Show the network connections on port 5432, the usual PostgreSQL port, and the program that holds each one.
sudo lsof -i :5432What you should see: Each connection on that port is listed, with the program that holds it.
Show every process holding one file open. Use the real path of a file of your own.
sudo lsof /srv/exports/orders.csvWhat you should see: Every process that holds the file open is listed.
List open files that were deleted but are still held. Their space is not freed while a program holds them.
sudo lsof +L1What you should see: Deleted files that a program still holds are listed, with the word deleted after the name.
Terms
- Open file
- A file, network connection or pipe that a running process has opened and is using, tracked by the kernel.
- File descriptor
- The number, or the special letters such as cwd, that names an open file inside a process, shown in the FD column.
Check yourself
Why does a full disk not show its space in the folders?
A deleted file keeps its space while a program still has it open. The folders no longer list the file, but the disk still holds its space.
Which option finds who holds a network port?
The i option, with a colon and the port number. For example, lsof -i :5432 shows the holders of port 5432.
Why run lsof with sudo?
Without sudo, lsof shows only your own files, so other users' open files stay hidden and the picture is incomplete.
Previous: The sys folder: the kernel's view of devices. Next: What does systemd do?.