Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files being used by a unix process

The fuser command lets me know which processes are using a file or directory.

I'm looking for command that does the opposite: lets me know which files are being used by a process.


Update

Forgot to mention that it's for a Solaris system.

like image 287
lamcro Avatar asked Nov 19 '08 07:11

lamcro


People also ask

How do I list open files for a process in Unix?

UNIX List Open Files For Process First use the ps command command to get PID of process, enter: $ ps -aef | grep {process-name} $ ps -aef | grep httpd

What are the list of Unix and Linux commands?

Conclusion Category List of Unix and Linux commands Documentation help • man • pinfo Disk space analyzers df • duf • ncdu • pydf File Management cat • cp • mkdir • tree Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHE ... 9 more rows ...

How do processes start in Unix Linux?

Whenever a command is issued in unix/linux, it creates/starts a new process. For example, pwd when issued which is used to list the current directory location the user is in, a process starts. Through a 5 digit ID number unix/linux keeps account of the processes, this number is call process id or pid. Each process in the system has a unique pid.

Is there a command that lets you see which processes are using files?

The fuser command lets me know which processes are using a file or directory. I'm looking for command that does the opposite: lets me know which files are being used by a process. Forgot to mention that it's for a Solaris system. Show activity on this post.


2 Answers

lsof -p <pid>

From here

lsof stands for “LiSt Open Files”. This shell command seems deceptively simple: It lists information about files opened by processes on a UNIX box.

Despite its (apparent) modest mission statement, lsof is actually one of the most powerful and useful UNIX commands. Its raw power comes from one of UNIX’s design principle often described as ”in UNIX everything is a file”. What this means is that the lsof concept of an open file not only covers regular files but also the following:

  • Directories
  • Streams or network files (for example, Internet or UNIX domain sockets and NFS files)
  • Native libraries (for example, .soor .dylibdynamic libraries linked to a process)
  • Block and character special files (for example, disk volume, external hard drive, console, or mouse)
  • Pipes

Wait, I Cannot Find lsof on My System!

lsof is such a popular tool that it has been ported to pretty much all UNIX dialects (Linux, Mac OS X, BSD, Solaris, and so on). If it is unavailable on your box, use your usual package management system to install it. You can find lsof packages for Solaris on Sun Freeware.

like image 111
Johannes Schaub - litb Avatar answered Nov 02 '22 20:11

Johannes Schaub - litb


While I wouldn't begrudge anyone learning Dtrace or gaining experience installing software, in Solaris there is a command to see the files a process has open: /usr/bin/pfiles

% tail -f /etc/motd &
[1] 6033

% pfiles 6033
6033:   tail -f /etc/motd

      Current rlimit: 256 file descriptors
       0: S_IFREG mode:0644 dev:182,65538 ino:163065 uid:0 gid:3 size:54
          O_RDONLY|O_LARGEFILE
          /etc/motd
       1: S_IFCHR mode:0620 dev:299,0 ino:718837882 uid:101 gid:7 rdev:24,3
          O_RDWR|O_NOCTTY|O_LARGEFILE
          /dev/pts/3
       2: S_IFCHR mode:0620 dev:299,0 ino:718837882 uid:101 gid:7 rdev:24,3
          O_RDWR|O_NOCTTY|O_LARGEFILE
          /dev/pts/3
like image 27
tpgould Avatar answered Nov 02 '22 19:11

tpgould