我们如何获得正在运行的应用程序的命令行?

在Ubuntu中,可以从终端打开应用程序。 但有时候不清楚适当的命令是做什么的。

因此,打开一个应用程序,我怎样才能获得用于启动它的命令,而无需在任何地方搜索(只需查看它)?

我刚刚制作了以下脚本,使用应用程序窗口标题找出正确的命令,从终端打开相应的应用程序(我将其命名为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 

将此脚本保存在~/bin目录中,不要忘记使其可执行:

 chmod +x ~/bin/appcmd 

用法:

  • 当脚本被破坏而没有任何参数时,脚本将返回所有打开的窗口的相应命令。

  • 如果给出任何参数,脚本将尝试找到一个打开的应用程序窗口,其中包含该标题的参数,并将返回相应的命令。 例如,如果Chromium浏览器处于打开状态,您可以找到仅使用以下命令从终端打开它的命令:

     appcmd chromium 

从这里 :

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

如果您只需要启动命令行,那么只需:

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

运行命令后,只需单击要显示启动命令的窗口。

另一种脚本:

 #!/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 . 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 

用法:

  1. 将上述脚本保存到文件中并使其可执行。
  2. 双击并选择“运行”来运行该文件。
  3. 单击您想要了解命令的窗口。
  4. 信息将显示给您。 (标题,PID,ID,类和命令行)
  5. 您可以单击“复制”按钮将命令行复制到剪贴板。
    这需要xsel 安装xsel 要安装。

在此处输入图像描述

作为替代方案而不需要脚本,您只需打开系统监视器并将鼠标hover在您想要了解命令行的过程上。

如果启用“依赖关系视图”,您将能够看到哪个进程称为另一个进程,例如,您可以看到Chrome为每个选项卡创建的各种进程,并将其追溯到具有命令行的父进程用户(用户)调用了哪些Chrome。

我发现的最相似的想法是xwininfo,它为您提供有关正在运行的窗口的信息。 但它并没有告诉你里面运行的是什么程序。

列出正在运行的进程的命令名称和参数的另一种方法是:

 ps axk pid,comm o comm,args > output.txt 

(重定向到文件中,以便不截断命令名称/参数。)

来源: man ps :examples部分(稍作修改)。

系统监视器是ps的GUI。