AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 13582
Accepted
David Oneill
David Oneill
Asked: 2010-11-17 16:21:49 +0800 CST2010-11-17 16:21:49 +0800 CST 2010-11-17 16:21:49 +0800 CST

激活(如果已经运行,则运行或置于前面)程序的命令

  • 772

我希望能够有一个键盘快捷方式来启动或将程序置于前面。(如果重要,sqldeveloper)

我知道如何运行它,留下:

1)如何确定程序是否已经在运行?

2)如何将程序带到其他打开的窗口的其他顶部?

~~编辑~~

感谢您到目前为止的回复。当我运行其中任何一个命令来获取正在运行的进程列表时,我得到以下信息:

doneill   3492     1  0 Nov16 ?        00:00:00 /bin/sh /usr/local/bin/sqldeveloper
doneill   3493  3492  0 Nov16 ?        00:00:00 /bin/bash /opt/sqldeveloper/sqldeveloper.sh
doneill   3495  3493  0 Nov16 ?        00:00:00 bash sqldeveloper
doneill   3552  3495  1 Nov16 ?        00:25:07 /usr/lib/jvm/java-6-sun-1.6.0.22/bin/java -Xmx640M -Xms128M -Xverify:none -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Dsun.java2d.ddoffscreen=false -Dwindows.shell.font.languages= -XX:MaxPermSize=128M -Dide.AssertTracingDisabled=true -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Djava.util.logging.config.file=logging.conf -Dsqldev.debug=false -Dide.conf="/opt/sqldeveloper/sqldeveloper/bin/sqldeveloper.conf" -Dide.startingcwd="/opt/sqldeveloper/sqldeveloper/bin" -classpath ../../ide/lib/ide-boot.jar oracle.ide.boot.Launcher

我确实通过 .sh 脚本启动它。它是一个基于java的程序。其中哪一个是我感兴趣的?我尝试使用xdotool来提高它,但我还没有成功,尽管我今晚没有时间深入研究手册页。

如果我运行xdotool search Orac('Oracle SQL Developer' 是窗口标题的开始),它会返回一堆数字。其中之一是否代表我感兴趣的内容?

scripts command-line xfce
  • 3 3 个回答
  • 5954 Views

3 个回答

  • Voted
  1. Best Answer
    SiliconChaos
    2010-11-17T18:16:20+08:002010-11-17T18:16:20+08:00

    一点 bash 和xdotool应该可以解决问题。在帖子末尾安装 xdotool 的注意事项。

    激活或启动.sh

    #!/usr/bin/env bash
    
    # usage:
    #    ActivateOrLaunch.sh firefox-bin
    #
    # Will launch firefox-bin or activate the first window in the windows stack
    # --- Remember to chmod a+x ActivateOrLaunch.sh in order to execute.
    
    # pgrep looks through the currently running processes and lists the process
    #       IDs which matches the selection criteria to stdout
    #       for more information on pgrep http://linux.die.net/man/1/pgrep
    
    # if process is found by pgrep then pipe through head to get firt instance in
    #    case of multiple processes running. If process not found $pid will be empty
    pid=$(pgrep ${1} | head -n 1)
    
    # Using "-z" check if $pid is empty, if so execute the process
    if [ -z "$pid" ]; then
       echo "$1 not running... executing..."
       $1 &
    else
       # process was found, find the first visible window and activate it
       echo "Found process $1 with PID $pid"
    
       # using xdotool [http://www.semicomplete.com/projects/xdotool/] get the first
       # visible windows using $pid. Redirect stderr to /dev/null and only select
       # the first visible windows using head
       wid=$(xdotool search --onlyvisible --pid $pid 2>/dev/null | head -n 1)
    
       # if $wid is empty the process does not have any visible windows... do nothing
       if [ -z "$wid" ]; then
         echo "Didn't find any visible windows for process $1 with PID: $pid"
       else
         # send the window id ($wid) from the window stack to xdotool
         echo "Activating first windows in stack"
         xdotool windowactivate $wid
       fi
    fi
    
    # ******** NOTES **********
    # In order for this script to work correctly you need to pass the complete process 
    # name for it to find any visible windows. The process needs needs to be in the path
    # for it to execute if not running.
    #
    # For example
    #
    # If you try it with firefox-bin on Ubuntu 10.10 it will find the running process and 
    # activate the window, but it will not be able to launch the process since the executable 
    # in the path is called firefox which is a link to firefox.sh
    # (/usr/bin/firefox -> ../lib/firefox-3.6.12/firefox.sh) which in turn executes firefox-bin
    # (not in path).
    #
    # Next, if you use firefox it will find a process but won't be able to find any windows 
    # since it's a wrapper function calling firefox-bin and doesn't have any windows. 
    

    你可以在github上找到代码

    我在 Ubuntu 10.10 (Gnome) 上测试了代码,它工作正常。它需要一些润色,因为我只是把它打了出来,并不担心让它漂亮(想确保得到评论)

    要安装 xdotool 你可以apt-get install xdotool(给我这个获取的版本 2.20100701.2691),如果你想要最新最好的从这里获取它(截至 20101116 它是版本 2.20101012.3049-4)

    • 6
  2. Jeremy
    2010-11-17T16:34:10+08:002010-11-17T16:34:10+08:00

    在您的主目录中创建一个名为 sql-raise.sh 的文件

    #!/bin/sh
    如果 ps -ef | grep 进程名 | grep -v grep ; 然后
        #进程正在运行,把它带到最前面
        xdotool search --name 进程名 windowraise
        出口 0
    菲
    
    #进程没有运行,启动它
    进程名
    

    开始的行if ps -ef...转换为:

    • 列出所有正在运行的进程
    • 过滤掉任何不包含的行process-name
    • 过滤掉grep命令本身,防止误报

    起始行xdotool是将过程带到最前面的行。

    在命令行上执行chmod +x ~/sql-raise.sh以使文件可执行。

    要将其绑定到键,请使用系统 -> 首选项 -> 键盘快捷键

    • 2
  3. turbo
    2010-11-18T07:00:23+08:002010-11-18T07:00:23+08:00

    'launchorsitch.sh firefox' 如果 firefox 没有运行,则启动它,如果它不处于活动状态,则切换到它;如果它是活动窗口,则将其最小化。

    #!/bin/bash
    # usage: "launchorswitch.sh xterm" to change to/start/hide xterm
    app=$1
    app_win_id=`wmctrl -lx|grep -i $app|cut -d ' ' -f 1`
    case $app in
        terminator)
            app_exec="terminator --geometry=1000x720+140+28"
        ;;
        *)
            app_exec=$app
        ;;
    esac
    if [ -z $app_win_id ]; then
        $app_exec & # app not started, so start it
    else
    
        active_win_id=`wmctrl -r :ACTIVE: -e 0,-1,-1,-1,-1 -v 2>&1|grep U|cut -d ' ' -f 3`
        if [ $app_win_id == $active_win_id ]; then
            wmctrl -r :ACTIVE: -b toggle,hidden    # hide app when active
        else
            wmctrl -i -a $app_win_id    #switch to app
        fi;
    fi;
    

    一开始有程序的特殊规则(这里是终止符)。我的 hack 使用 wmctrl 并允许为要查找/运行的可执行文件指定特殊规则。欢迎对风格、明显错误等提出任何意见。

    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve