Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In Ubuntu, applications can be opened from a terminal. But sometimes it isn't clear what the appropriate command is to do this.

So, having an application open, how can I get the command used to launch it, without having to search anywhere (just by looking at it)?

share|improve this question
add comment

6 Answers

up vote 7 down vote accepted

I just made the following script which use the application window title to find out the right command which opens the respective application from terminal (I named it appcmd):

#!/bin/bash

#appcmd - script which use the application window title to find out the right command which opens the respective application from terminal

#Licensed under the standard MIT license:
#Copyright 2013 Radu Rădeanu (http://askubuntu.com/users/147044/).
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

#check if wmctrl is installed
if [ ! -n "$(dpkg -s wmctrl 2>/dev/null | grep 'Status: install ok installed')" ]; then
    echo -e "The package 'wmctrl' must to be installed before to run $(basename $0).\nUse 'sudo apt-get install wmctrl' command to install it."
    exit
fi

window_title=$(echo $@ | awk '{print tolower($0)}')
windows=$(mktemp)
pids=$(mktemp)
pid_found=""

wmctrl -l | awk '{$2=$3=""; print $0}' > $windows

cat $windows | while read identity window; do
    if [[ $(echo $window | awk '{print tolower($0)}') == *$window_title* ]]; then
        wmctrl -lp | grep -e "$identity.*$window" | awk '{$1=$2=$4=""; print $0}'
    fi
done > $pids

while read pid window; do
    if [ "$pid" != "0" -a "$window" != "Desktop" ]; then
        echo -e "Application window title:\t$window"
        echo -e "Command to open from terminal:\t\$ $(ps -o command $pid | tail -n 1)\n"
        pid_found="$pid"
    fi
done < $pids

if [ "$pid_found" = "" ]; then
    echo "There is no any opened application containing '$@' in the window title."
fi

Save this script in your ~/bin directory and don't forget to make it executable:

chmod +x ~/bin/appcmd

Usage:

  • When the script is runed without any argument, the script will return all commands for all windows opened corresponding.

  • If any argument is given, the script will try to find an open application window containing in its title that argument and will return the command corresponding. For example if Chromium browser is open, you can find out the command which opens it from terminal using only:

    appcmd chromium
    
share|improve this answer
    
@RaduRădeanu: working great.. :) RaduRădeanu, you are awesome..!! –  Saur.K Oct 6 '13 at 14:36
    
I haven't tried your script but does it return the switches that are used as well? For example, I launch leafpad, a text-editor, like this: leafpad --tab-width=2. Would your output include --tab-width=2? –  user25656 Oct 11 '13 at 6:54
    
@vesa1 Well, you can try it. I don't have leafpad installed right now, but for some applications yes, will return the arguments also. –  Radu Rădeanu Oct 11 '13 at 7:00
add comment
xprop | awk '($1=="_NET_WM_PID(CARDINAL)") {print $3}' | xargs ps h -o pid,cmd

from here. If you only need the starting command line then just:

xprop | awk '($1=="_NET_WM_PID(CARDINAL)") {print $3}' | xargs ps h -o cmd

After you've run the command just click on the window for which you want the starting command to be shown.

share|improve this answer
    
Thank you! I never would have figured this out on my own. –  AsianSquirrel Jan 13 at 23:29
1  
Would be sweet if this was included as an output of xwininfo... –  virtualxtc Jan 14 at 1:58
    
And what if I don't have a mouse? –  Radu Rădeanu Jan 25 at 23:52
    
@RaduRădeanu Then use your touchpad. :) Actually my answer was migrated from this duplicate question which explicitly asked for a click method. –  falconer Jan 26 at 0:07
add comment

As an alternative without needing a script, you can just open the System Monitor and hover your mouse over the process which you'd like to know the command line of.

If you enable "Dependencies view", you'll be able to see which process called another so, for example, you can see the various processes which Chrome creates for each tab and trace it back to the parent process which will have the command line which Chrome was invoked with (by the user).

share|improve this answer
    
This will not work all the time. A quick example is the default file manager from Ubuntu - Nautilus. –  Radu Rădeanu Oct 27 '13 at 7:36
    
@RaduRădeanu Of course it doesn't get the command from the alternate name (Files) of nautilus, but if you just want a simple way to get the arguments, this will work fine. You can still recognize the icon –  minerz029 Oct 27 '13 at 7:38
add comment

An alternative script:

#!/bin/bash

# Copyright © 2013  minerz029
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

shopt -s extglob

for var in 'wm_pid' 'wm_name' 'wm_class' 'cmdline' 'wm_id'; do
    declare "$var"'=Not found'
done

notify-send -t 3000 'Click on a window to get the command line...'
xprop_out="$(xprop)"

while IFS=$'\n' read -r -d $'\n' line; do
    if [[ "$line" == '_NET_WM_PID(CARDINAL) = '* ]]; then
        wm_pid="${line#_NET_WM_PID(CARDINAL) = }"
    elif [[ "$line" == 'WM_NAME('?(UTF8_)'STRING) = '* ]]; then
        wm_name="${line#WM_NAME(?(UTF8_)STRING) = }"
    elif [[ "$line" == 'WM_CLASS('?(UTF8_)'STRING) = '* ]]; then
        wm_class="${line#WM_CLASS(?(UTF8_)STRING) = }"
    elif [[ "$line" == 'WM_CLIENT_LEADER(WINDOW): window id # '* ]]; then
        wm_id="${line#WM_CLIENT_LEADER(WINDOW): window id # }"
    fi
done <<< "$xprop_out"

if [[ "$wm_pid" == +([0-9]) ]]; then
    quote () 
    { 
        local quoted="${1//\'/\'\\\'\'}";
        out="$(printf "'%s'" "$quoted")"
        if eval echo -n "$out" >/dev/null 2>&1; then
            echo "$out"
        else
            echo "SEVERE QUOTING ERROR"
            echo "IN: $1"
            echo -n "OUT: "
            eval echo -n "$out"
        fi
    }
    cmdline=()
    while IFS= read -d '' -r arg; do
        cmdline+=("$(quote "$arg")")
    done < "/proc/$wm_pid/cmdline"
fi

text="\
Title:
    $wm_name
Class:
    $wm_class
ID:
    $wm_id
PID:
    $wm_pid

Command line:
    ${cmdline[@]}"

copy() {
    { echo -n "$1" | xsel -i -b >/dev/null; } && xsel -k
}

if [[ -t 1 ]]; then
    echo "$text"
    if [[ "$1" == '--copy' ]]; then
        echo "Copied"
        copy "$cmdline"
    fi
else
    zenity \
        --title='Window information' \
        --width=750 \
        --height=300 \
        --no-wrap \
        --font='Ubuntu Mono 11' \
        --text-info \
        --cancel-label='Copy' \
        --ok-label='Close' \
    <<< "$text"
    if [[ $? == 1 ]]; then
        copy "$cmdline"
    fi
fi

Usage:

  1. Save the above script into a file and make it executable.
  2. Run the file by double-clicking and selecting "Run".
  3. Click on the window you would like to know the command of.
  4. Information will be displayed to you. (Title, PID, ID, class and command line)
  5. You may click the "Copy" button to copy the command line to the clipboard.
    This requires xsel Install xsel to be installed.

enter image description here

share|improve this answer
1  
very nice execution of shell scripting. +1 from me –  souravc Jan 14 at 3:33
add comment

The most similar think I've found is xwininfo, which gives you information about a running window. But it doesn't tell you what program is running inside it.

share|improve this answer
add comment

Another way to list the command name and arguments of the running processes is:

ps axk pid,comm o comm,args > <filename>

(Redirect into a file so the command names/arguments are not truncated.)

Source: man ps: examples section (with a little modification).

System monitor is a GUI for ps.

share|improve this answer
    
You can always use grep to find the command if you have a vague idea. –  minerz029 Oct 27 '13 at 7:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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