Ubuntu Tip: How To Launch Programs In The Background From A Terminal Window
When 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.
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.
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
To resume Firefox in the background, execute bg %1
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.
Filed under Ubuntu And Linux Tips by
Leave a Comment




Comments on Ubuntu Tip: How To Launch Programs In The Background From A Terminal Window
w3rd. Nice explanation.
Quick, easy, and not too much nonsense.
how do you open a gui application (ex. firefox) from the terminal???
when the command
'firefox &'
is givin the response is
"Error: no display specified"
how do i fix that?
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.
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.
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.
Thanks alot, just what I have been looking for.
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?
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$$.
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?
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!
@Anonymous:
so do you actually have something to suggest?
Send it to /dev/null, of course:
firefox & > /dev/null
of course.
thanks and I apologize for ruining your day with my ignorance and stupidity.
@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.
@Anonymous
You need to to get outside more if you believe that is true ; -)
@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.
@JP
Good point. This is the reason why idiots like Anonymous will never get it. All they do is cry and whine that linux is not the primary desktop used by consumers. Then when they see help being offered to newbies, they do nothing but bash and hide behind anonymous posts.
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!
@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.
@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.