Thursday, April 26, 2018

How to find an application's command, graphically or otherwise?



I often find that I need to figure out the command to execute a certain application, such as an Alarm Clock application or the System Monitor. However, many of the actual CLI commands to run these are not directly evident. What I usually do is just go to the Ubuntu Software Center, then search for the application's "common name" and hence find its package/command name. I would like to know if there is an easier or more efficient way of doing this.



I was wondering if there is a command similar to xprop, where clicking on an open (graphical) application would output its command in the terminal. In other words, I am interested in a graphical way of finding an application's command.




For example, if I run such a command and click on System Monitor, the terminal would output gnome-system-monitor, etc. Thanks.



PS. If a solution is not currently available, I would be interested in writing my own script with this functionality. I only know basic bash, however. I realize that should probably go under a separate question, but I'm just throwing it out there.



I wrote this (rather ugly) script to find the command to execute a running application by clicking on it. It uses xprop to get the "clicking interface" and to get information about the selected window:



#!/bin/bash

xprop > ./tmp


chk_desk=`grep -c "_NET_WM_DESKTOP_FILE(STRING)" ./tmp` #Checks whether application has a corresponding .desktop file. The '-c' counts the number of matches.

if [ $chk_desk -ne 0 ] ; then
desk_line=`grep "_NET_WM_DESKTOP_FILE(STRING)" ./tmp` #Extract line containing .desktop path
desk_path=${desk_line/"_NET_WM_DESKTOP_FILE(STRING) = "/} #Extract only the .desktop path
desk_path=${desk_path//\"/} #Removes quotes
exe_line=`grep "Exec=" $desk_path | head -n1`
exe=${exe_line/"Exec="/}
else #If .desktop doesn't exist, it uses the PID to find the executable
pid_line=`grep "_NET_WM_PID(CARDINAL)" ./tmp`

pid=${pid_line/"_NET_WM_PID(CARDINAL) = "/}
exe_path=`readlink -f /proc/$pid/exe`
exe=${exe_path/\/usr\/bin\//}
fi

echo $exe

rm ./tmp

exit 0



In case you are wondering, I could not have used only the PID for matching since /proc/$pid/exe gives you the path of the "real" executable, or of the underlying service running the program. So if you used it on say 'geogebra', the script would output java instead. Or in the case of Firefox you would get the path /usr/lib/firefox/firefox, which I suppose is where the actual firefox executable exists, which means the /usr/bin/firefox points to it.



While firefox is not a big problem (you could use the "real" executable), for the java based applications the .desktop file is needed to get the command to run the actual application and not a blank java session.



This script, of course, will only work if you are using X as your display server (since it uses xprop). I'm sure there must be a way to make the script cleaner, though my main problem was having to create many variables to hold the grep line outputs and the multiple substitutions.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...