4

The command to launch a program, say evince, from bash shell in background is

evince &

But what if I want to open some file with evince in background? Nor evince myfile.pdf & neither evince & myfile.pdf work as expected: the first outputs something like [2] 5356 but in the next line the shell returns locked; the second opens an empty istance of evince.

So, what syntax can I use?

2
  • 2
    The first one should work. Sometimes you don't get a prompt printed after starting a background process (seems to be a bug). Just press enter and the prompt should appear, if not then this is also a bug (though somewhat more serious).
    – Graeme
    Apr 10, 2014 at 18:24
  • Yes the prompt appears if I press enter one more time.
    – BowPark
    Apr 21, 2014 at 18:08

2 Answers 2

5

Sometimes, when you start a program, it prints out some message. If you start it in background, the program may lock up until you bring it back to the foreground so that it can display its message. The solution is to redirect stdout and stderr so that the program can continue running in the background. One way to do this is:

evince myfile.pdf >~/evince.errs 2>&1 &

The above creates a file in your home directory with whatever message evince wanted to display.

If you are convinced that evince's messages are unimportant, you can discard them without creating a file:

evince myfile.pdf >/dev/null 2>&1 &

After running either of the above commands, the shell should produce a message like [1] 1234 and then a shell prompt should appear. As Graeme suggests, if the shell prompt does not appear, try pressing enter again.

6
  • Background commands still have their output printed on the shell. All that redirection should not be necessary.
    – Darkhogg
    Apr 10, 2014 at 23:52
  • Also, instead of >some_file 2>&1 you can redirect both stdout and stderr at once using &>some_file.
    – Darkhogg
    Apr 10, 2014 at 23:53
  • Thank you, there where some message errors and so they disappeared, but the prompt doesn't appear.
    – BowPark
    Apr 21, 2014 at 18:05
  • @BowPark As a test, after starting the background job and when the prompt doesn't appear, try typing date and pressing enter. If you see the date output, that means that the prompt actually did return but, for some reason, it is not visible.
    – John1024
    Apr 21, 2014 at 22:52
  • You're right, the output is correct.
    – BowPark
    Apr 21, 2014 at 22:59
5
evince myfile.pdf

CTRL+Z

bg

That opens the program, suspends it, then backgrounds it.

Or:

( evince myfile.pdf ) &

Would open evince in a (subshell) and backgrounds the (subshell). It's a little weird that simply:

evince myfile.pdf &

doesn't work though because it does for me.

You might want to:

echo $-

And check your job control parameters. I recommend:

set -b

if you're not already using it. Do:

man set

for more info. Or see my other answer here.

1
  • I tried with -b but the shell behaves as before, like in all the other solutions. Thank you anyway! Maybe my shell has a bug. When I launch a program in backgroud, if then i press Enter the prompt appears, like written in a comment to my question.
    – BowPark
    Apr 21, 2014 at 18:03

You must log in to answer this question.

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