As a system Administrator at SingleHop, one common type of scenario that we run into almost every day is when a client will call or send us a ticket saying âHelp!Â My server is slow and weâre not sure why!â Depending on the server OS, there are a lot of different ways to approach troubleshooting this type of issue. Â This post will concentrate on Linux-based systems.
When troubleshooting performance on most Linux based servers, one of my favorite tools to use is lsof, which stands for list open files.Â Sure, there are some simpler ways to look at similar information, such as top, which shows all the top processes in real time, or ps auxÂ which will show you ALL the processes and their status.Â Â However, when the answer isnât obvious or you need more information about what exactly is bogging your server down, lsof is a great tool to get started.
Let me give you an example that I ran into recently while on shift. Â A client sent a ticket in which his server memory usage was spiking and running entirely too high. The server's IOwait% was spiking, basically, he was almost out of memory and his hard drive was being used heavily to compensate.Â I accessed the server via SSH and ran a topÂ command. In the output, I noted that there were two processes that were using the majority of the server's memory, both were zip.Â In most Linux screens, on the far left, youâll note PID, or process ID.Â For this example, letâs assume a PID of 12345
At the command line, we run a simple command:
Â lsof Â -p 12345
The output you'll get a list of ALL the files being held open by that process.Â You will often see a lot of log files and system files there, but in the haystack there will be a needle... usually it will reveal the source of the problem.
Back to the example I spoke of earlier. In the lsofÂ output, we found that the process was actually zipping up some large SQL dump files from the customer's eCommerce software.Â We looked at the software log files and identified who started the process.
Here are a few other things you might have a lot of luck doing with lsof:
Finding and stopping compromised scripts.
If your server is rooted or hacked, itâs pretty common for the people who did this to dump different scripts inside hidden folders buried far within your system folders.Â Iâve used lsof several times to help weed out scripts buried deep within system files and stop/delete them. For example, letâs say there is a rogue perl script running some port scans or being used to DDoS another machine, ps aux | grep perl to get the PID followed by a lsof âp PIDwill help you drill down exactly where the script is and from there you can delete the script and start figuring out how to fix the problem.
Find out what is running on a certain port.
This is very useful when you note some bizarre things in a netstat ânacommand.Â There are several ways to do this, but letâs say you want to see what is listening on port 2525 of your server. You could do this:
lsof -Pnl +M -i4 | grep :2525
or a simpler version:
lsof âi :2525
See what a files a system user has open.Â
So, letâs say we want to see what files the user âarchieâ is currently using on the server
Lsof âu archie
Finding out what is running when trying to unmount a device.Â
Ever try to unmount a CD Drive, virtual drive, or even a USB thumb drive and you get the pesky âDevice is busyâ error message.Â lsof can help here too.Â If you are trying to unmount /dev/sda3
lsof is really that simple and thatÂ powerful as troubleshooting tool. Â If you use it in conjecture with other commands, such as grep, then it can really make for some powerful one line pieces of code.