2

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 '18 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 '18 at 11:37
2

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.

0

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

sudo fuser -fu [FILEPATH AND FILENAME]

F.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .