3

For example:

$ cd ~/code/someFolder
~/code/someFolder$ gitk --all& # launches a gtk window of gitk
[1] 9335 # what is this in the first place

now I go do some work inside the gtk window, then close it

~/code/someFolder$ echo 'foobar'
foobar
[1]+  Done                    gitk --all # this extra output

I have an inkling that the ampersand is somehow 'holding' the process and closing it in gtk ends the process resulting in the output... but that's a very unscientific explanation.

PS: Sorry about the unwieldy question, I couldn't think of a better way to express it.

7
  • Read this question and this other one for context.
    – edwin
    Jan 31, 2014 at 22:47
  • Have a go with these resources: mit.edu and wooledge. The short answer is that suffixing & sends the job to the background, you can run the command jobs to see the jobs on the background and fg N to send the N-th job to the foreground.
    – edwin
    Jan 31, 2014 at 22:51
  • Awesome, so that's [jobNumber] PID: first part of the question solved :)
    – Aditya M P
    Jan 31, 2014 at 23:45
  • Notice that this is not isolated to "Gtk commands", and that the extra output does not depend on you running a new command, you can just press the Enter key. What's happening is that bash is notifying you about the commands you cannot "see" (because they are on the "background"). For example, you can start a big download in the background wget http://my.download.com/file & and when it's finished bash will notify you so with something like [N]+ Done <command + parameters you used>.
    – edwin
    Jan 31, 2014 at 23:50
  • [...] If you don't send the download to the background, you would have to wait for it to finish before being able to run another command.
    – edwin
    Jan 31, 2014 at 23:57

1 Answer 1

2

In your question, there are some misconceptions. Firstly, this behavior is not limited to Gtk commands. Secondly, the notification is not send after the "next command" but on the next prompt (say, as soon as you type the Enter key).

What you are doing is called Job Control. In bash you can send commands to the "background" so that they don't block the command line. For example, try running gedit on the terminal. Gedit's window should appear and the command line should be "blocked", that is, you cannot type any new command unless you close Gedit.

A solution to this blocking behavior of "long running" processes is to send their commands to the background on invocation. This way the command won't block and you will be able to continue working on the command line.

A way to send a command to the background is by using the & after the command. Continuing with the Gedit example, you can start it on the background by running

gedit &

You will now notice that the command prompt is printed immediately and you can run new commands (contrast this with simply running gedit without ampersand). You'll also notice the extra information about the new "job" you just created: the job number in brackets, and its PID.

When the job ends (either normally or because you killed it), bash will notify you with something similar to:

[1]+  Done                    gedit &

(The 1 is the job number and on the right you have the command with which you created this job, just in case you forgot :-)

If you have several jobs on the background you can use the command jobs to get a list with their statuses.

In conclusion, you don't need to add the ampersand unless you want to run another command immediately.

For references and other interesting aspects about job control read:

2
  • I think OP mentioned GTK because running gtk apps from a terminal tends to throw up more "messages" in the terminal than a CLI "app" does. In most cases, these messages appear when the program is launched. I'm assuming that OP has encountered some gtk apps that throw up messages in the terminal when exiting as well.
    – user25656
    Feb 1, 2014 at 2:03
  • @vasa1 I know, but OP only mentions the messages related to sending the process to the background :)
    – edwin
    Feb 1, 2014 at 2:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .