Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to be able to have a keyboard short-cut to either start or bring to the front a program. (sqldeveloper if it matters)

I know how to run it, that leaves:

1) how do I find out if a program is already running?

2) how do I bring the program the other top of other open windows?

~~ edit ~~

Thanks for the responses so far. When I run either of those commands to get the list of running processes, I get the following:

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/
doneill   3495  3493  0 Nov16 ?        00:00:00 bash sqldeveloper
doneill   3552  3495  1 Nov16 ?        00:25:07 /usr/lib/jvm/java-6-sun- -Xmx640M -Xms128M -Xverify:none -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Dsun.java2d.ddoffscreen=false -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

I do launch it via the .sh script. It is a java based program. Which of those are the one I'd be interested in? I tried using the xdotool to raise it, I haven't been successful yet, although I don't have time tonight to dig into the man pages.

If I run xdotool search Orac ('Oracle SQL Developer' is the start to the title of the window), it returns a bunch of numbers. Do one of these represent what I'm interested in?

share|improve this question
up vote 5 down vote accepted

A little bash and xdotool should do the trick. Notes on installing xdotool at end of post.

#!/usr/bin/env bash

# usage:
# firefox-bin
# Will launch firefox-bin or activate the first window in the windows stack
# --- Remember to chmod a+x 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

# 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 &
   # process was found, find the first visible window and activate it
   echo "Found process $1 with PID $pid"

   # using 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"
     # send the window id ($wid) from the window stack to xdotool
     echo "Activating first windows in stack"
     xdotool windowactivate $wid

# ******** 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
# (/usr/bin/firefox -> ../lib/firefox-3.6.12/ 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. 

You can find the code on github

I tested the code on Ubuntu 10.10 (Gnome) and it worked. It need some polishing since I just typed it up and wasn't worried about making it pretty (wanted to make sure to get the comments in)

To install xdotool you can apt-get install xdotool (form me this fetched version 2.20100701.2691), if you want the latest and greatest get it from here (as of 20101116 it's as version 2.20101012.3049-4)

share|improve this answer
Aww, that's better than mine :) Nice, stealing it! – Jeremy Nov 17 '10 at 2:26
I'm loading it up to github probably by tomorrow... I'll post the link as a comment. Need to clean it up a little first. – SiliconChaos Nov 17 '10 at 3:33
As it turns out, I'm trying to use this script to map to a media key on my keyboard. So, I can just hardcode it to search for the exact phrase. – David Oneill Nov 18 '10 at 15:08

Create a file in your home directory called

if ps -ef | grep process-name | grep -v grep ; then
    #The process is running, bring it to the front
    xdotool search --name process-name windowraise
    exit 0

#The process is not running, start it

The line starting if ps -ef... translates to:

  • List all processes which are running
  • Filter out any lines which don't contain process-name
  • Filter out the grep command itself, to prevent false positives

The line starting xdotool is the one which brings the process to the front.

On a command line execute chmod +x ~/ to make the file executable.

To bind this to a key, use the System -> Preferences -> Keyboard Shortcuts

share|improve this answer
To check what the process-name should be, run ps -ef on the command line and scroll through it looking for your process. – Jeremy Nov 17 '10 at 0:39

' firefox' starts firefox if it isn't running, switches to it, if it isn't active or minimizes it if it's the active window.

# usage: " xterm" to change to/start/hide xterm
app_win_id=`wmctrl -lx|grep -i $app|cut -d ' ' -f 1`
case $app in
        app_exec="terminator --geometry=1000x720+140+28"
if [ -z $app_win_id ]; then
    $app_exec & # app not started, so start it

    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
        wmctrl -i -a $app_win_id    #switch to app

At the beginning there are special rules for programs (here it's terminator). My hack uses wmctrl and allows to specify special rules for what executable to look for/run. Any comments on style, obvious mistakes etc welcome.

share|improve this answer

Your Answer


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.