I have c++ application and I need to know if one file is in use by anyone. I'm ok with running any shell command, but I don't want to run lsof command for this purpose, since I think I can't safely assume that lsof will be available on all OS, or can I safely assume that?

  • Comments are not for extended discussion; this conversation has been moved to chat. – bmike Aug 10 at 11:33
  • We close questions that ask multiple related questions. Feel free to look at Stack Overflow for your code specific questions - anything about API / SDK other than - where do I get them is off topic here. Have a careful read of help center here and as well. The rules do get enforced since these sites are high visibility and high traffic. Consider you're asking this in central square - NYC - a very public display. Lastly, and most importantly Welcome to Ask Different. – bmike Aug 10 at 11:37
up vote 2 down vote accepted

You can assume that lsof ships on all macOS and you can also check the existence of that command before you call it, logging a nice friendly error message and or log message as you see fit if you want to code defensively.

You could also ask on a code-focused site what API exist to check for file existence if you want to go into specific C++ code (which would be off topic here), but from your question assuming you can call any command line tool, your overall design is fine to execute in a C++ program on macOS.

My friend, you might use lsof, or the builtin ‘fuser’.

sudo fuser -fu [FILEPATH AND FILENAME]

F.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.