Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

10.5: Find out which program/process is locking a file System 10.5
Leopard only hintSometimes the trash won't be emptied, or a volume refuses to eject with a "file in use error." There have been hints on how to find that out in Terminal with lsof, which is not necessarily for the faint of heart.

Here is a nice Automator workflow that runs some AppleScript and shell commands and can be used in contextual Finder menu. So when a file is reported used, control-click on it and run the Automator workflow and see who is to blame. It will use user process space (no prompt for admin password), but will also revert to system process space if needed (prompting for the password). It will display the application/process that is using the file, and the account that started the program (could be you, could be root, could be another local user).

Open Automator with empty workflow, then add following actions, in this order:
  1. Get Selected Finder Items
  2. Sort Finder Items (you can omit that, if you want)
  3. Run AppleScript
In the body of the AppleScript action, replace the text that's there with the following:
on run {input, parameters}
repeat with aFile in input
  set theFile to quoted form of POSIX path of aFile
  set cmd1 to "lsof -t " & theFile
  try
    set thePid to do shell script cmd1
    on error number errorNumber
    try
      set thePid to ""
      set thePid to do shell script cmd1 with administrator privileges
    end try
  end try
  try
    if thePid is "" then
      set theMessage to theFile & " is not in use."
    else
      set cmd2 to "ps -o comm= -A " & thePid
      set theProgram to do shell script cmd2
      set cmd3 to "ps -o ruser= -A " & thePid
      set theOwnerName to do shell script cmd3
      set theMessage to theFile & " used by: " & thePid & " (" & theOwnerName & ")
" & theProgram
    end if
  end try
  display dialog theMessage buttons {"OK"}
end repeat
return theMessage
end run
Save it as a Finder Plug-in giving it a meaningful name, then quit Automator. Use it by Control-clicking the file that you want inspected (in Finder), then choose More » Automator » Find Buys (or whatever you named the plug-in).

[robg adds: I can't get this working on my 10.5 box, but it works fine for the author, so I'm running it under the assumption that I've done something to break it on my test machine. For me, the dialog appears empty, and Automator returns an error about not getting the selection from the Finder, even though I can display the value of the selected item within Automator.]
    •    
  • Currently 3.27 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (15 votes cast)
 
[4,800 views]  

10.5: Find out which program/process is locking a file | 14 comments | Create New Account
Click here to return to the '10.5: Find out which program/process is locking a file' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
10.5: Find out which program/process is locking a file
Authored by: fresler on Mon, Nov 23 2009 at 7:44AM PST
If you're using Snow Leopard, select Service from the template dialog box and set the options at the top to "files or folders" and "Finder". Then all you need to do is bring over the Run Applescript action.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: nova20 on Mon, Nov 23 2009 at 9:24AM PST
I created this as a Service in Snow Leopard and it works very well. I saved it as "Who's-Locking-This". It is placed automatically in the user's ~/Library/Services. You can move it manually to /Library/Services for all users.

A slightly quicker way of bring up the Service template is to right-click on any item (not on an empty space) in the Finder, scroll down to the Services submenu and select Create Service.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: UberFu on Mon, Nov 30 2009 at 8:10AM PST
Snow Leopard improves the reliability of ejecting discs and external drives. Sometimes when an application or process is using the files on a drive, Mac OS X prevents you from ejecting it, but you don’t always know why. In Snow Leopard, you’ll get fewer of those errors and when you do get them, you’ll see exactly which application is using the drive, so you can quit it and eject the drive properly.
I haven't had a chance yet to really get into SL but Apple has noted in the past and the above text is currently on their website that the system is supposed to now point out what is causing a Disk Eject hang-up. I have known from past experience that past Apple "features" like this tend to be watered down and intended for very very basic use. So I'm assuming that from all the Script writing and Automator stuff - that it either doesn't work or it sucks? It would be nice if we could get something to work smoothly in Leo tho.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: nova20 on Wed, Dec 2 2009 at 3:29AM PST
That's about right about some Apple features. In this case the new SL feature tells you the name of the item in use. It's more than before but it could do better. The script by boris42 gives the full path of that item, the Pid, the user name owning it and the application name.
The full path is really useful when you have more than one volume, while the user name helps to avoid deleting something owed by another user on your mac.

Regarding Leopard, the Automator workflow works well provided you make it in Leo (you cannot use it in Leo if it was made in SL) and install it in ~/Library/Workflows/Applications/Finder/. The workflow will be available from the Contextual Menu Items/More/Automator.


[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: SOX on Mon, Nov 23 2009 at 8:44AM PST
Here's the workflow that I use

1) ASK FINDER FOR ITEMS
Start at /Volumes
Type: Files and Folders (Allow multiple selection)

2) RUN SHELL SCRIPT
shell: /usr/bin/perl
pass input as arguments

for $f (@ARGV) { $r = quotemeta $f; $c = `basename $r`; print `lsof +fg | grep -v " EVO " | grep "$r"`};

3) ASK FOR TEXT
text: "The Culprits"
default text: "Nothing Found"
require an answer

When you run this, it opens /volumes and lets you pick the disk you cannot eject. then it tells you which apps are locking it.


[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: SOX on Mon, Nov 23 2009 at 8:46AM PST
By the way this workflow should work better than the original hint because it removes a bunch of false positive hits. LSOF will report apps that have files open in a non-binding sort of way. the perl script in the above automater will strip those out.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: joshforman on Mon, Nov 23 2009 at 9:02AM PST
Or you can use this donationware app.
---
Josh Forman

http://www.jofo.com/


[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: Dr. T on Mon, Nov 23 2009 at 10:16AM PST
I agree. "What's Keeping Me" works well and is easier than creating scripts and services.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: chadvonnau on Mon, Nov 23 2009 at 11:26AM PST
Not trying to cause FUD, but that app never worked for me in a meaningful way. It seems like it's doing what it's supposed to, but it never actually helped me "unstick" anything on the few occasions I tried it. I think that stuck apps/volumes are a problem where it's usually easier and, in my experience, more reliable to just restart.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: Wenga on Mon, Jan 18 2010 at 1:45PM PST
Also have a look at Lock-unmatic. Its a gui for lsof, and displays which processes are blocking the files.
http://www.macupdate.com/info.php/id/33303/lock-o-matic

Unfortunately, it does sometimes have problems with special chars in filenames.

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: Fairly on Mon, Nov 23 2009 at 11:29AM PST
Not for the faint of heart? But this "script" is just a glorified wrapper for more command line stuff so what's the point?

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: ewelch on Mon, Nov 23 2009 at 7:11PM PST
What's the use? What computers are best at! Helping you avoid repetitive tasks that you would otherwise have to do manually. That's what's the use!
---
Eric

Ernest Hemingway's writing reminds me of the farting of an old horse. - E.B. White


[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: cxd101 on Tue, Nov 24 2009 at 8:40AM PST
I got this to work under 10.6, by first opening a blank Automator "service" vs. a blank workflow - i.e.

Open Automator with empty service

CD

[ Reply to This | # ]
10.5: Find out which program/process is locking a file
Authored by: rocketmouse on Tue, Nov 24 2009 at 11:47AM PST
@ chadvonnau
[re: What's Keeping Me?.app]
I've found that always using As Administrator (i.e. check marking the box) and then using "Kill" rather than "Quit" works reliably for me.

[ Reply to This | # ]