Ubuntu Tip: How To Launch Programs In The Background From A Terminal Window

33

ubuntulogo.jpgWhen running programs from an terminal window in Ubuntu, the program process will run in the foreground, occupying the terminal session that started the program. Until the program ends or is closed, you can not start other applications as long as this process is running in the foreground. While you can open another terminal window or use the screen command, Bash shell provides an easier function known as job control that will allow multiple programs to run in the background and return control to the terminal window on Ubuntu.

To use job control and allow programs to return control back to the terminal window, commands entered should be followed with the ampersand character &

For example, you want to open Firefox. From the command line you would type:

firefox &

Firefox will open in the background and the terminal window control will be returned allowing you to continue working in the terminal window. If you want to open another program or run a script, you can enter the command follow by &

For longer running programs started in the background, which do not need user input, job control commands can be executed that can show status, suspend, bring a program to the foreground, and return to background.

In the screen shot below, Firefox and Terminal server client applications were started in the background. Executing the jobs command displays all running jobs started in the background.

ubuntujobs1.png

To bring the first program started in the background – Firefox, to the foreground and then suspend the process, execute
fg %1
followed by Ctrl+Z

ubuntujobs2.png

To resume Firefox in the background, execute bg %1

ubuntujobs3.png

Below are all options that can be used with job control:

jobs [-lnprs] [jobspec]
jobs -x command [arguments]

The first form lists the active jobs. The options have the following meanings:

-l
List process IDs in addition to the normal information.

-n
Display information only about jobs that have changed status since the user was last notified of their status.

-p
List only the process ID of the job's process group leader.

-r
Restrict output to running jobs.

-s
Restrict output to stopped jobs.

If jobspec is given, output is restricted to information about that job. If jobspec is not supplied, the status of all jobs is listed.

If the `-x' option is supplied, jobs replaces any jobspec found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.

bg [jobspec]

Resume the suspended job jobspec in the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if jobspec was not found or jobspec specifies a job that was started without job control.

fg [jobspec]

Resume the job jobspec in the foreground and make it the current job. If jobspec is not supplied, the current job is used. The return status is that of the command placed into the foreground, or non-zero if run when job control is disabled or, when run with job control enabled, jobspec does not specify a valid job or jobspec specifies a job that was started without job control.

Ctrl +Z Suspend (stop, but not quit) a process running in the foreground (suspend).

Ctrl +C Interrupt (terminate and quit) a process running in the foreground.

Comments on Ubuntu Tip: How To Launch Programs In The Background From A Terminal Window Leave a Comment

April 2, 2008

ENDER @ 1:39 pm #

w3rd. Nice explanation.
Quick, easy, and not too much nonsense.

June 26, 2008

how @ 1:27 am #

how do you open a gui application (ex. firefox) from the terminal???

how#2 @ 1:32 am #

when the command

'firefox &'

is givin the response is

"Error: no display specified"

how do i fix that?

September 19, 2008

man_on_the_street @ 10:31 am #

You were probably trying to start firefox from the console, either
1.) as a different user than the one who is running the xserver, e.g. after doing "su", or
2.) while logged in remotely through an SSH session.

In case 1.) you canuse the programme "sux" to switch users. It allows programmes, launched by the user you change to, to display windows on your X server.

In case 2.) enable X server forwarding through SSH by adding
-o ForwardX11=yes
to your SSH command on the command line. If you want to enable this by default for all SSH sessions, you can add the line
ForwardX11 yes
to your ~/.ssh/config file. The latter is potentially dangerous, though.

December 31, 2008

dave @ 3:59 pm #

Well actually there is a third way, what I did:

Start a process from an init script in /etc/init.d that then tries to launch firefox.

Any ideas on how to get the display working in this case? I tried "sux" but hav't got it working, obviously the ssh option won't work here.

are there any other ways? I need something that will work being run as a command in an init script.

Dave.

January 26, 2009

Tintin @ 8:40 pm #

You should be able to get the X window on your screen by running ssh -Y servername. My understanding is that the -Y option passes the output to the remote host.

July 10, 2009

Nadinengland @ 7:41 am #

Thanks alot, just what I have been looking for.

September 30, 2009

Anonymous @ 12:04 pm #

This is such a horribly newbish post. ALMOST a newbish as pointing out that you can run programs from the terminal.

Of course, this is an Ubuntard post, so I shouldn't be surprised. Is Canonical that determined to make Ubuntu so idiot friendly that even the most basic Linux skills are passed over?

November 13, 2009

Nanonymous @ 9:22 am #

Does ctrl-C work for useless posts like yours? Get a grep you cron-magnon. Aux, did I hit sore spot? Why don't -u pico on someone your own size. Geez, looking at the title (of the thread), it IS a newbish post; so why did you bother stopping by…and how does the arpanet look through a terminal? Get your head out of the matri…er…I mean your a$$.

January 9, 2010

stathiz @ 2:54 pm #

Thank you very much for the post.
When I try to run firefox in background using
firefox &
it keeps printing output information on the terminal, so its not entirely on the background. If i type a command it is being executed but when a new output is created it is printed again on the screen…

Any idea why this is happening?

Anonymous @ 2:57 pm #

Because you're still running it from the terminal Nothing about putting th & at the end tells the program NOT to use stdout.

Thiis post still reeks of Ubuntard. Holy crap! you can run x programs from a terminal! And in the background, too!

stathiz @ 3:21 pm #

@Anonymous:

so do you actually have something to suggest?

Anonymous @ 3:46 pm #

Send it to /dev/null, of course:

firefox & > /dev/null

stathiz @ 3:53 pm #

of course.
thanks and I apologize for ruining your day with my ignorance and stupidity.

Anonymous @ 4:02 pm #

@stathiz: Nah, wasn't you. Was the article. Was so ludicrously obvious is all. Anyone who uses Linux for more than three minutes figures that out.

January 14, 2010

JP @ 1:01 pm #

@Anonymous:
Of course the article "reeks of ubuntard". Even waaaaay more obvious than running processes in the background is the fact that this was written for those who didn't know before. In order to know the information that was detailed in this article, someone needs to be told by someone or some resource. I highly doubt anyone who has 1) not already been told and 2) has been using linux for less than 3 minutes will say "oh I wonder what happens if I stick a random symbol after a command. Let's try a '&'" followed by "lets try more random letters and see if it does anything. I'll try 'fg %1'"

Anyways, my point is that yes, this article is geared towards linux newbies (which you were at one point in the past!) so let's let the linux newbies soak it up without having you tell them they're total idiots for not already knowing it, k? Since they're here, they obviously want to learn it. Let's not discourage that.

April 4, 2010

ubuntardX @ 7:15 pm #

Great little tutorial, really well explained and exactly what I need for ssh terminal connections. Thanks man articles like this are really making getting to grips with terminal a lot easier. Ready to start playing with Ubuntu server edition soon!

May 21, 2010

Steven P @ 6:27 am #

@Anonymous:

Greetings. Rather than berating a man for not knowing how to fish, why not show him how to bait a hook? You and your ilk add way too much background noise.

July 26, 2010

Robert @ 1:21 pm #

@man_on_the_street:

"In case 1.) you canuse the programme "sux" to switch users. It allows programmes, launched by the user you change to, to display windows on your X server."

I know this is a really old post…but Google led me here. Just wanted to say THANK YOU for this! Somehow I'd never had this problem before; sux was a great, simple solution.

February 25, 2011

Jan @ 9:00 pm #

Sorry but I don't get it. I have installed Linux (Centos) for the first time and am trying to get from the CLI to the GUI at 192.168.1.4:8088/static/config/index.html via firefox. (I know the URL is OK because i succeded in Lynx but I am hoping firefox is going to be more user friendly.) There is only one user (root) at this stage. So when I type firefox & I get the above error "no display specified". I tried to specify stdout but may have used the wrong syntax. I have installed xorg. What do I do?

February 26, 2011

Yaro Kasear @ 9:06 am #

@Jan: Yeah, you're definitely new to Linux.

Just installing Xorg is a step. But X doesn't automatically run just because you run an X-based app. When they say "launch programs from the terminal window" they mean from a terminal emulator on an X desktop.

I don't know how CentOS handles automatically launching X, but you're going to need to do a few things:

1. STOP using single mode. It's for system maintenance and repair ONLY. Running as root is a Bad Thing. Make sure you're on runlevel 3 or 5 before trying anything not system-critical. Runlevels are the "modes" by which Linux and UNIX boot, run, and shut down and are rather important. Runlevel 3 is the standard CLI multi-user mode, generally, it doesn't launch X, but it's safe for users to use the system. Runlevel 5 is the default X bootup runlevel, so, instead of booting in runlevel 1, boot runlevel 5.

2. Cent OS is not really a desktop Linux. It's most definitely not for new Linux users. It's primarily aimed at servers, hence why it won't run X by default. I recommend Mint or Ubuntu for new Linux users.

3. On your current setup, once you're in the correct runlevel (NOT single user mode, and NOT logged in as root.) and you're not running X, you'll need a window manager or desktop environment installed. X alone will not provide a complete desktop, as X is primarily just a windowing system, an interface for the clients (The GUIs.) to talk to display and input hardware.

4. In runlevel 3, you need to make sure you have an .xinitrc in your home directory, that at least contains: exec or an inittab describing which display manager (Login screen) to launch at boot time. This is, for the record, ALL somewhat advanced Linux stuff. This is why desktop distros are good for starting out in Linux because they handle getting Xorg launched at boot for you.

My recommendation: Install Ubuntu and learn Linux. Then switch to another distribution not aimed at servers.

Jan @ 6:01 pm #

Thanks Yaro. My reason for using Centos is that I am really interesting in Asterisk which came as a bundle (AsteriskNOW) and is a telephony server system. I have downloaded Ubuntu! and will eventually be using it for other purposes including getting familiar with Linux. In the meantime I think I will forget about the GUI and use the vi editor to set up the config files in Asterisk.

Jan @ 6:05 pm #

Correction of typo: …interested in Asterisk…

Dave @ 7:36 pm #

Hi Jan,

You might want to try nano instead of vi, In my experience it is easier to use.

Good luck,

Dave.

February 27, 2011

Yaro Kasear @ 9:18 am #

@Jan: I recommend trying to learn Linux before trying to use it as a server for anything.

December 9, 2011

Kyle @ 1:42 pm #

Wow I'm late at reading this lol.

@Jan:

Runlevels:
0 – Halt
1 – single user mode (Yaro is right, should only be used for maintenance, CLI)
2 – multi user without networking (CLI)
3 – multi user with networking (CLI)
4 – not used/user defined
5 – multi user with display manager (GUI)
6 – system reboot

"~$" – home directory of a "normal user", "~#" – home dir of admin user (root)

The Community ENTerprise Operating System, or CentOS, is an amazing distro I might say. By default runlevel 5 in CentOS is the GUI and there are a couple different ways you can get to runlevel 5:

1.) ~$ init 5
2.) *if using vi or vim, cause vi rules* ***can be dangerous***
~# vi /etc/inittab
– change "id:3:initdefault:" to "id:5:initdefault:" #this will boot up into runlevel 5
or just type ":%s/id:3:initdefault:/id:5:initdefault:/c" enter "y" at the prompt then
type "ZZ" to save and quit.
3.) ~$ or ~# startx
4.) when booting up press the TAB key when prompted (or press "e".. I can't remember) and add "5" at the end of the line. Press the ENTER key then press "b" if ENTER doesn't start the boot process.

Remember this is if you're using CentOS.

January 4, 2012

Rex @ 12:54 pm #

Thanks Kyle for final closer,
I am a Google surfer and I end up to this post after one of my search.
While reading this i feel people have lots of "Use less" Time that they point each other naked a$$eS [apologies for putting comment like that] but at some point of time "Anonymous" is right at his stand, cause its impossible you do Google or go through the doc you won't find the solution.
I a beginner too but I like to do research before creating a new blog.

June 27, 2012

Flyer @ 5:37 pm #

I ssh to a remote server and submit a task on remote console that will run in background using following command

$ task &

Now when I log out from remote server, the task I submitted in background is aborted.

What am I doing wrong? how can I make the task to continue running even after I log out from the server.

Thanks

June 29, 2012

Kyle @ 2:37 pm #

@Flyer:

Really if you want to run a process in the background without being interupted even if you logout I would recommend screen. But if you're stubborn (most of us GNU peeps are) then, man crontab, cron can run scripts at particular times of days,weeks,months, and so on. But see when you run, ~$ test &, or ./test (if it's a script) and Ctrl+Z to suspend the process, one could run a simple ps command and see that the "test" CMD is running on TTY pts/0. So when you logout the terminal connected to the standard input is no longer there (aka pts/0) therefore the process running on pts/0 is gone because pts/0 is no longer active. The screen command leaves a TTY open and accessible after logging out (assuming you disconnected from the screen session properly before logging out, ctrl+a d). Also, if you do user: ~$ nohup task & then you can logout and the process will write all output to nohup.out in the home directory which can be reviewed at a later time.

Flyer @ 6:05 pm #

@Kyle: Thanks a lot. Your reply was very helpful. I am using screen to run process in the background. I read this blog to learn details about screen command.

http://www.thegeekstuff.com/2010/07/screen-command-examples/

June 30, 2012

Kyle @ 11:23 am #

@Flyer:

Glad to see the information helped out. All of us in the IT community love the screen command (when it comes to us Linux die hards). That is a very good blog that does explain a good bit about screen but remember that if you're going to use Linux then there are something you should never forget. If you need quick help with a command do $ –help, that will normally give the user a quick help list of options. If '–help' doesn't cut it then the man command is your best friend $ man screen, will show you the manual page for screen. Now if you don't know what the command is but you know what it should do then $ apropos , will search all the man pages installed on the system by the headers and anything having to do with the description will show up. Take it easy and I hope that helps.

Regards,
Kyle
Tyler VPS
tylervps.com (coming soon)

Leave a Comment

Fields marked by an asterisk (*) are required.

Subscribe without commenting