Command of the Day: fuser
May 2, 2007
This one is pretty cool – fuser identifies the process id of any processes that are using a specific file or filesystem. This is something I need frequently on Windows when debugging service or multi-user tests. Invariably there will be a race condition that is causing cleanup to not succeed because the cleanup thread has gotten ahead of the app or test thread(s). When that happens the cleanup thread is unable to move or delete log files, temp data, etc.
I’m left wondering who had the file open. There are many ways to find this but I normally use a SysInternals tool to check which process has the open handle. Problem is this isn’t easy to automate so by the time I’m looking for the file lock it’s already gone.
Using fuser I can add this functionality right into my test suites.
To test fuser I performed a more operation on a file in my home directory and left the more process blocked with the file open. That command was:
username@desktop:~$ more menu.lst
In another terminal window I then executed:
username@desktop:~$ fuser -u menu.lst menu.lst: 5829(username)
So process 5829 being run by the user “username” has the file menu.lst open.
Now I can use ps to figure out what command that user is running (and on what tty).
username@desktop:~$ ps 5829 PID TTY STAT TIME COMMAND 5829 pts/1 S+ 0:00 more menu.lst
I think I need to try piping all of that together into one operation.