Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lsof should give all open files for a set of pids

I have 30 instances of a process running on a server and want to log open files for each process for analysis.

I ran the following command:

* ps auwwx | grep PROG_NAME | awk '{print $2}' | xargs lsof -p | less

It complaints that, "lsof: status error on : No such file or directory"

However, if I run lsof -p < pid > it gives me the list of open files for that process . How can I get a list of all open files for all 30 instances of the process on a FreeBSD machine.

Moreover, I do not want the shared libraries to be listed. If I do -d "^txt" it isn't showing some other db files which I want to be shown. Is there any other way to grep out the .so files?

like image 325
user1071840 Avatar asked Sep 17 '12 23:09

user1071840


People also ask

How do I list all open files by a specified user?

“lsof” stands for List Open Files. It is a Linux utility for listing down all the open files of a system. This command can be combined with different parameters to modify its output as desired.

What does the lsof command do?

The lsof command stands for LiSt Open Files and shows open files and which process uses them. Since Linux sees every object as a file, such as devices, directories, etc., unidentified open files prevent users from modifying them. Additionally, the sheer number of files makes it difficult to find malicious processes.

Which command is used to list of all opened files?

lsof stands for List Open Files. It is easy to remember lsof command if you think of it as “ls + of”, where ls stands for list, and of stands for open files. It is a command line utility which is used to list the information about the files that are opened by various processes.

How do you list out all files that are open by processes running on the Linux system?

To find out the list of files opened by parent process Id lsof command is used with the option -R.


1 Answers

The lsof -p option takes a comma-separated list of PIDs. The way you're using xargs will pass the pids as separate arguments leading some to be interpreted as filenames.

Try lsof -p $(your grep | tr '\012' ,) That's going to have a trailing comma, I'm not sure if lsof will care but you could sed it off if necessary.

like image 156
Ben Jackson Avatar answered Sep 26 '22 06:09

Ben Jackson