I would like to get a list of open files in a process on os x (10.9.1). In Linux I was able to get this from /proc/PID/fd. However I'm not sure how to get the same on OS X. I found that the procfs is not present on the OS X (by default. possible implementations present, but I do not want to go that way).
So how do I get (natively) the list of open files in a process on OS X. One way is lsof. is there any other support available? please let me know where I can get more info on this.
Thanks.
I had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo). The original question mentions lsof, and it does the trick exactly. If you know the process name or PID, it's super quick.
Find processes by name:
lsof -c processname
Find processes by PID:
lsof -p 123
(Prefix with sudo as needed, such as if you are not the owner of the process.)
At least on OSX 10.10 (Yosemite, didn't check on Mavericks), you can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup.
Tip: cmd+f shortcut on that pane allows for searching and highlighting on the content.
This works for some things:
sudo fs_usage | grep dev
for /dev/ files or similar.
The clean and simple approach to inspect the current process (i.e. the equivalent of /proc/self/fd on Linux) is to use ls /dev/fd/:
e.g.
$ touch "file"
$ exec 3<>file
$ ls /dev/fd/
0 1 2 3
List open files on /Volumes/VolumeName:
lsof | grep "/Volumes/VolumeName"
"You can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup."
But "you had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo) or others user".
So just run Active Monitor by sudo, that`s it
sudo /Applications/Utilities/Activity\ Monitor.app/Contents/MacOS/Activity\ Monitor
I use the What's Open application that is very handfull (with filters, disk selection, ...).
You can find it there : http://whatsopen.en.softonic.com/mac.
Since you asked "Is there any other support [than lsof] available?", try this:
Create a command line tool using the "proc_pidinfo" C API referenced in the selected answer to this question: How can I programmatically get the list of open file descriptors for a given PID on OS X?
You can use proc_pidinfo with the PROC_PIDLISTFDS option to enumerate the files used by a given process. You can then use proc_pidfdinfo on each file in turn with the PROC_PIDFDVNODEPATHINFO option to get its path.
lsof -c /^74016$/ -d^txt -FcfadDtns0
The -F instructs lsof to produce output suitable for consumption by another process, such as Perl or awk. In man lsof search for OUTPUT FOR OTHER PROGRAMS for details. The characters that come after -F represent a field you want to select for output. The output then puts each of these characters in front of the respective field. Example output:
p212^#cloginwindow^#
fcwd^#a ^#tDIR^#D0x1000004^#s704^#n/^#
f0^#ar^#tCHR^#D0xf218cacb^#n/dev/null^#
f1^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f2^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f3^#ar^#tREG^#D0x1000004^#s216424^#n/Library/Application Support/CrashReporter/SubmitDiagInfo.domains^#
f4^#ar^#tREG^#D0x1000004^#s77^#n/private/etc/security/audit_user^#
f5^#ar^#tREG^#D0x1000004^#s652^#n/private/etc/security/audit_class^#
f6^#ar^#tREG^#D0x1000004^#s358^#n/private/etc/security/audit_control^#
f7^#ar^#tREG^#D0x1000004^#s111033^#n/System/Library/Frameworks/CoreImage.framework/Versions/A/Resources/ci_stdlib.metallib^#
f8^#au^#tIPv4^#d0xc401abd77f1dd1d9^#n*:*^#
f9^#ar^#tREG^#D0x1000004^#s308316^#n/System/Library/Frameworks/CoreImage.framework/Versions/A/Resources/ci_filters.metallib^#
f10^#au^#tREG^#D0x1000004^#s1536^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/3902/libraries.maps^#
f11^#au^#tREG^#D0x1000004^#s65536^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/3902/libraries.data^#
f12^#au^#tREG^#D0x1000004^#s1536^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/Intel(R) HD Graphics 630/functions.maps^#
f13^#au^#tREG^#D0x1000004^#s131072^#n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/Intel(R) HD Graphics 630/functions.data^#
p421^#ccoreauthd^#
fcwd^#a ^#tDIR^#D0x1000004^#s704^#n/^#
f0^#ar^#tCHR^#D0xf218cacb^#n/dev/null^#
f1^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f2^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
p537^#cUserEventAgent^#
fcwd^#a ^#tDIR^#D0x1000004^#s704^#n/^#
f0^#ar^#tCHR^#D0xf218cacb^#n/dev/null^#
f1^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f2^#au^#tCHR^#D0xf218cacb^#n/dev/null^#
f3^#au^#tunix^#d0xc401abd77b9c8579^#n->0xc401abd77b9c8709^#
f4^#au^#tunix^#d0xc401abd77b9c7129^#n->0xc401abd77b9c8899^#
Related
I actually wrote a script that is compiling my LaTeX files and open the generated PDF in a viewer. That works fine.
cd Documents/my-bachelor-thesis/
latexmk 000_Root_Bachelor_Thesis.tex -pdf
open 000_Root_Bachelor_Thesis.pdf
ps -A | grep -m1 vorschau | awk '{print $1}'
So with the last line I get the PID of the process my PDF is opened.
There is the problem: I want stop my script process at the point the PDF is open. After I click on the close sign of the viewer, the process should continuo automatically. Is that somehow possible.
Current solution: I interrupt the process while waiting for some user input. After i type in something the process goes on.
echo "Can I proceed?"
read input
... more script
Thanks for helping me out.
Not familar with open in OSX. Quick search on man page suggests that you can force open to start new application, and wait until it finish by
open -Wn 000_Root_Bachelor_Thesis.pdf
Options:
-W Wait until the applications exit (even if they were already open). Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable.
-n Open a new instance of the application(s) even if one is already running.
Basic problem
I'm looking for a solution to enumerate all applications installed on a (Mac) OS X system, i.e. all application bundles registered with LaunchServices.
Simple approach that did not work
(Please note: lsregister can be found under /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister).
Currently I am using the output of lsregister -dump | grep -E '^.*[ \\t]*path:[ \\t]+(\/.*)$' | grep -Eo '\/.*', which has a few problems:
.app bundles with newlines (\n, which is valid on UNIX) are not handled correctly (This issue would be fixable by writing a proper parser)
lsregister doesn't handle correctly weird filename (e.g. containing the RTL mark). lsregister just seems to ignore files with the RTL mark in their name.
If you want to, you can create a file with RTL in its name by running the following command.
python -c 'import shutil; shutil.copytree(u"/Applications/TextEdit.app", u"/Applications/ThisIsAWeird\u202EApp.app")'
After doing this, lsregister -dump | grep ThisIsAWeird will not reveal it. But when you right click on a text file and go to "Open With" you'll that LaunchServices and Finder still support that file.
Simply enumerating all files in /Applications and other directories does not work because LaunchServices is aware of application bundles in arbitrary folders and I also need these apps.
Other approch that I didn't get to work
Apparently, there's a private API function called _LSFindApplications which does exactly what I need to do but I can't get it to work and didn't find any inofficial documentation on the internet. (proof it exists: nm /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/LaunchServices | grep __LSFindApplications$)
UPDATE: system_profiler method also doesn't seem to work
Until now I though the system_profiler method as suggested by #Anoop works but today I found out that lsregister still picks up more binaries. Some or missing on all machines, some seems to be only missing in system_profiler when you have mounted an external volume with another installation of OS X. Here's a list what's missing in the output of system_profiler (but is present in lsregister) on one of my machines:
/Applications/Automator.app/Contents/Resources/Application Stub.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Framework.framework/Resources/app_mode_loader.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Framework.framework/Resources/crash_report_sender.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Helper EH.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Helper NP.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Helper.app
/System/Library/CoreServices/AddPrinter.app
/System/Library/CoreServices/AirPlayUIAgent.app
/System/Library/CoreServices/AppleFileServer.app
/System/Library/CoreServices/AppleGraphicsWarning.app
/System/Library/CoreServices/AppleScript Runner.app
/System/Library/CoreServices/AppleScript Utility.app
/System/Library/CoreServices/CalendarFileHandler.app
/System/Library/CoreServices/Certificate Assistant.app
/System/Library/CoreServices/CoreLocationAgent.app
/System/Library/CoreServices/CoreServicesUIAgent.app
/System/Library/CoreServices/CoreTypes.bundle
/System/Library/CoreServices/Database Events.app
/System/Library/CoreServices/DiskImageMounter.app
/System/Library/CoreServices/Expansion Slot Utility.app
/System/Library/CoreServices/File Sync.app
/System/Library/CoreServices/FileSyncAgent.app
/System/Library/CoreServices/Folder Actions Dispatcher.app
/System/Library/CoreServices/Folder Actions Setup.app
/System/Library/CoreServices/HelpViewer.app
/System/Library/CoreServices/Image Events.app
/System/Library/CoreServices/Install in Progress.app
/System/Library/CoreServices/Jar Launcher.app
/System/Library/CoreServices/Java Web Start.app
/System/Library/CoreServices/KeyboardSetupAssistant.app
/System/Library/CoreServices/Language Chooser.app
/System/Library/CoreServices/LocationMenu.app
/System/Library/CoreServices/MRTAgent.app
/System/Library/CoreServices/Memory Slot Utility.app
/System/Library/CoreServices/NetAuthAgent.app
/System/Library/CoreServices/Network Diagnostics.app
/System/Library/CoreServices/Network Setup Assistant.app
/System/Library/CoreServices/ODSAgent.app
/System/Library/CoreServices/Pass Viewer.app
/System/Library/CoreServices/Printer Setup Utility.app
/System/Library/CoreServices/RegisterPluginIMApp.app
/System/Library/CoreServices/Screen Sharing.app
/System/Library/CoreServices/SecurityAgent.app
/System/Library/CoreServices/SecurityFixer.app
/System/Library/CoreServices/SocialPushAgent.app
/System/Library/CoreServices/System Events.app
/System/Library/CoreServices/System Image Utility.app
/System/Library/CoreServices/SystemUIServer.app
/System/Library/CoreServices/Ticket Viewer.app
/System/Library/CoreServices/UniversalAccessControl.app
/System/Library/CoreServices/UnmountAssistantAgent.app
/System/Library/CoreServices/UserNotificationCenter.app
/System/Library/CoreServices/VoiceOver.app
/System/Library/CoreServices/ZoomWindow.app
/System/Library/CoreServices/backupd.bundle/Contents/Resources/TMLaunchAgent.app
/System/Library/CoreServices/loginwindow.app
/System/Library/CoreServices/rcd.app
/System/Library/Frameworks/PubSub.framework/Versions/A/Resources/PubSubAgent.app
/System/Library/Frameworks/Quartz.framework/Versions/A/Frameworks/QuickLookUI.framework/Versions/A/Resources/QuickLookUIHelper.app
/System/Library/Frameworks/QuickLook.framework/Versions/A/Resources/quicklookd.app
/System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/Resources/DiskImages UI Agent.app
/System/Library/Services/AppleSpell.service
/System/Library/Services/ChineseTextConverterService.app
/System/Library/Services/ImageCaptureService.app
/System/Library/Services/OpenSpell.service
/System/Library/Services/SpeechService.service
/System/Library/SocialServices/Facebook.socialplugin
/System/Library/SocialServices/Twitter.socialplugin
/System/Library/SocialServices/Weibo.socialplugin
First, your request is not quite right:
I'm looking for a solution to enumerate all applications installed on a (Mac) OS X system, i.e. all application bundles registered with LaunchServices.
Those are not the same thing. The application bundles registered with LaunchServices is the output of lsregister -dump. That is not the same thing as "all applications installed." There is no exact query for "all applications installed." OS X does not even really have such a concept.
The output of system_profiler is equivalent to what Spotlight knows about. That's not exactly the full list of installed applications, and may not be correct in several cases. Spotlight may have been disabled, for instance, or it may not have completed its first scan. (There is no supported way to ask Spotlight if it has finished its first scan; radar 15177626.)
There is also pkgutil --pkgs which will provide you additional "installed things." It isn't complete either, and may list things that are not installed.
You can also use mdfind to walk the whole disk and see what calls itself an application. This is pretty effective, but slow.
You can also use locate .app | grep '\.app$' which is pretty effective if the locate database is up to date and you only want app bundles.
I asked Apple about this at WWDC. There is no simple, supported answer. Open a radar. You can ask to dupe 15177489.
in terminal:
system_profiler SPApplicationsDataType > list_of_all_apps.txt
... writes output of the system_profiler query to that text file.
What about find using Cocoa application/code as done here.
Also, you might be able use to use system_profiler -xml SPApplicationsDataType and then parse the resulting XML data to get just the applications that you are interested in.
"open -a" is not the answer wanted, because I want to debug the Mac OS X application automatically. This means it's better if someone can give the command line like [program] [args] format. So ltrace mechanism can make [program] as target for debugging and take [args] as input.
I have tried command line like "/Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint" /Users/poc.pptx, only Microsoft Point process started but the poc.pptx not opened.
After grepping the Microsoft Point with pptx file opened, it's something like: /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint -psn_0_307275, there is no argument "poc.pptx".
I even manually use "gdb /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint" and "set args /Users/poc.pptx", and then "r", the target application can not run with the certain file opened.
I am confused about this, so, is there someone can help me to solve this problem?
Thank you!
open -b com.microsoft.PowerPoint <filename> seems to work for me to open presentations from the command line.
Go to file directory and then type
open -a "Microsoft PowerPoint" <filename.ppt>
Here "Microsoft PowerPoint" is the name of power point application, please check name of power point if it is different in your application directory.
This is working perfectly fine on my MAC (OSX 10.8).
We can also give complete path instead of just file name.
open -a "Microsoft PowerPoint" <ppt file path>
This is also working fine.
I know this is a old question, but here is my 2ct anyway.
I add the applications I want to open through command line in /usr/local/bin as a symlink.
I never run into any problems, but as Ken stated it depends how a application handles arguments.
Example with Visual Studio:
First I check what makes the application start bij executing the file inside the App contents like:
$ /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
If that works, then I create the symlink as follows (ln -s <path-to-app> <path-to-symlink>):
$ ln -s /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron /usr/local/bin/vs
After that I can start up Visual Studio with the current folder loaded as:
~/Development/SomeProject $ vs .
If PowerPoint is not opening a document passed as a command-line argument, then that's a reflection on how PowerPoint was coded. There's nothing anybody but Microsoft can do about that.
The OS does not normally use that technique to tell applications to open documents. Instead, it passes Apple Events to the application. Cocoa will, by default, accept command-line arguments and treat them similarly to such Apple Events, but apparently PowerPoint is overriding that default behavior.
If you want to debug or trace PowerPoint, I recommend that you do it in two steps. First, launch it without arguments under the debugger or trace program. Then, tell it to open a document. You can do that in the normal way, using the Finder and/or Dock, or you can use open -a .... Such a request to open a document will not launch a second instance of PowerPoint, it will deliver an event to the already-running PowerPoint which you are debugging/tracing. So, the result should be similar to what you seem to want.
Not sure if this will help you (depends on how you want to do your debugging), but you can use AppleScript from the command line, like this:
%osascript <<<EOD
tell application "Excel" to open "Users:xxx:Documents:sheet.xls"
EOD
When entered this way, your script can contain several lines, it does not have to be limited to a single one.
I want to launch an app on OSX from a script. I need to pass some command line arguments. Unfortunately, open doesn't accept command line args.
The only option I can think of is to use nohup myApp > /dev/null & to launch my app so it can exist independently of the script that launches it.
Any better suggestions?
As was mentioned in the question here, the open command in 10.6 now has an args flag, so you can call:
open -n ./AppName.app --args -AppCommandLineArg
In OS X 10.6, the open command was enhanced to allow passing of arguments to the application:
open ./AppName.app --args -AppCommandLineArg
But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.
An application bundle (.app file) is actually a directory. Instead of using open and the .app filename, you can move into the app's directory and start the actual machine code program located inside. For instance:
$ cd /Applications/LittleSnapper.app/
$ ls
Contents
$ cd Contents/MacOS/
$ ./LittleSnapper
That is the actual binary executable that might accept arguments (or not, in LittleSnapper's case).
In case your app needs to work on files (what you would normally expect to pass as: ./myApp *.jpg), you would do it like this:
open *.jpg -a myApp
You can launch apps using open:
open -a APP_YOU_WANT
This should open the application that you want.
open also has an -a flag, that you can use to open up an app from within the Applications folder by it's name (or by bundle identifier with -b flag). You can combine this with the --args option to achieve the result you want:
open -a APP_NAME --args ARGS
To open up a video in VLC player that should scale with a factor 2x and loop you would for example exectute:
open -a VLC --args -L --fullscreen
Note that I could not get the output of the commands to the terminal. (although I didn't try anything to resolve that)
I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:
> Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts
I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)
Lots of complex answers when you can simply access Applications folder and type:
open -a [APP NAME]
This is it!
I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications, regChrome for normal browsing and altChrome to use the special profile, to be easily started by keying ⌘-space to bring up Spotlight, then 'reg' or 'alt', then Enter.
I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.
What I ended up with was two AppleScript applications containing two commands each. Here is the one for altChrome:
do shell script "cd /Applications/Google\\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns"
do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"
The second line starts Chrome with the alternate profile (the --user-data-dir parameter).
The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.
Beginning with OS X Yosemite, we can now use AppleScript and Automator to automate complex tasks. JavaScript for automation can now be used as the scripting language.
This page gives a good example example script that can be written at the command line using bash and osascript interactive mode. It opens a Safari tab and navigates to example.com.
http://developer.telerik.com/featured/javascript-os-x-automation-example/
osascript -l JavaScript -i
Safari = Application("Safari");
window = Safari.windows[0];
window.name();
tab = Safari.Tab({url:"http://www.example.com"});
window.tabs.push(tab);
window.currentTab = tab;
Simple, here replace the "APP" by name of the app you want to launch.
export APP_HOME=/Applications/APP.app/Contents/MacOS
export PATH=$PATH:$APP_HOME
Thanks me later.
With applescript:
tell application "Firefox" to activate
Why not just set add path to to the bin of the app. For MacVim, I did the following.
export PATH=/Applications/MacVim.app/Contents/bin:$PATH
An alias, is another option I tried.
alias mvim='/Applications/MacVim.app/Contents/bin/mvim'
alias gvim=mvim
With the export PATH I can call all of the commands in the app. Arguments passed well for my test with MacVim. Whereas the alias, I had to alias each command in the bin.
mvim README.txt
gvim Anotherfile.txt
Enjoy the power of alias and PATH. However, you do need to monitor changes when the OS is upgraded.
To Create a New Text File OR open an existing one, in any folder, using a Text/Code Editor like the Free TextMate app on MACOSX, use this command on Terminal:
open -n /Applications/TextMate.app --args "$PWD/some file.txt"
Instead of a Text File, you can use any file type, based on your app's requirements and its support for this syntax.
This command also simulates the New Text Document Here Command on Windows and has been tested on MacBook Pro 2021 and Monterey 12.2.1 successfully.
the background is a shell script to open the .m3u file of a web radio station. Therefore I want to know inside the script, what's the user's program to open such files. At the moment, he has to set the environment variable $PLAYER, but obviously that is not a good way to go.
Alternative: Is there a command that takes a filename and searches itself for an appropriate program to handle that file? Like file, e.g.,
open-file my_playlist.m3u
The script should be portable, it will run at least on Ubuntu, Debian and Windows/Cygwin machines.
Cheers,
This will have to be done differently on each platform. On Mac OS X the "open" command will do what you want.
In Linux it gets murky, since the desktop environment (GNOME or KDE) keeps its own list of applications to run for each file type.
There are two files you can look for in Ubuntu / GNOME that hold this info:
~/.local/share/applications/defaults.list and
~/.local/share/applications/mimeinfo.cache
Someone else hopefully knows how to do this in Windows and can chime in.
Edit: Stealing from the other answers:
Linux:
xdg-open [filename]
Cygwin:
cygstart [filename]
And for completeness, here's a link to a previous question about how to detect which operating system you are running on: Detect OS from bash Script
I'd like if there were a different answer to this but I think you'll have to check the file association configs for every desktop environment and file manager out there (so, nautilus, konqueror, thunar, mc... all in different places and in different formats AFAIK), as well as ascertaining which one of these the user is actually using...
If someone has a different idea I'm keen to hear it.