Ubuntu – start a program from the terminal and make it launch visually in the background

command linescripts

If I open a program, like Firefox, from the terminal then that program will get the focus of the GUI. Can I somehow open it so that it will run in the visual background and not pop up, blocking any current program I am working with?

My problem is that I use karma test runner which is run every time I edit my code. Whenever that is done karma launches a browser which pops up in front of everything else on the screen which can be quite annoying.

I am using Ubuntu 13.04.

Best Answer

To do this, you will need xdotool. To install it, run the following command in terminal:

sudo apt-get install xdotool

Now, I will explain how you can do it for Firefox using a script:

  • In a terminal run:

    mkdir -p bin
    

    This command will make a bin directory in your home folder if you don't already have it.

  • After run:

    gedit ~/bin/firefox.sh
    

    This will create the new file change_sources.sh in gedit.

  • Copy and paste the following script in the new created file:
#!/bin/bash

delay=2
windowId=$(xdotool getwindowfocus)

firefox &
sleep $delay

xdotool windowactivate $windowId
  • Save the file and close it.
  • Go back into terminal and run:

    chmod +x ~/bin/firefox.sh
    

    to grant execute access for the script.

  • Run the script with:

    firefox.sh
    

In the same manner you can open any other application from terminal in the background.

Related Question