46

I'm currently running a process with the & sign.

$ example &

However, (please note i'm a newbie to Linux) I realised that pretty much a second after such command I'm getting a note that my process received a stopped signal. If I do

$ jobs

I'll get the list with my example process with a little note "Stopped". Is it really stopped and not working at all in the background? How does it exactly work? I'm getting mixed info from the Internet.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
user1255410
  • 856
  • 1
  • 9
  • 15

4 Answers4

98

In Linux and other Unix systems, a job that is running in the background, but still has its stdin (or std::cin) associated with its controlling terminal (a.k.a. the window it was run in) will be sent a SIGTTIN signal, which by default causes the program to be completely stopped, pending the user bringing it to the foreground (fg %job or similar) to allow input to actually be given to the program. To avoid the program being paused in this way, you can either:

  1. Make sure the programs stdin channel is no longer associated with the terminal, by either redirecting it to a file with appropriate contents for the program to input, or to /dev/null if it really doesn't need input - e.g. myprogram < /dev/null &.
  2. Exit the terminal after starting the program, which will cause the association with the program's stdin to go away. But this will cause a SIGHUP to be delivered to the program (meaning the input/output channel experienced a "hangup") - this normally causes a program to be terminated, but this can be avoided by using nohup - e.g. nohup myprogram &.

If you are at all interested in capturing the output of the program, this is probably the best option, as it prevents both of the above signals (as well as a couple others), and saves the output for you to look at to determine if there are any issues with the programs execution:

nohup myprogram < /dev/null > ${HOME}/myprogram.log 2>&1 &
twalberg
  • 59,951
  • 11
  • 89
  • 84
  • There's no need for `nohup` here. The _only_ thing it does you can't do with redirections alone is refuse to forward HUP signals, but `disown -h` will do that and is built into bash itself. (And HUP signals aren't sent in common noninteractive cases in the first place, so the problem `nohup` solves _doesn't even exist_ unless you're at an interactive terminal rather than a script). – Charles Duffy Jun 24 '23 at 16:29
2

Yes it really is stopped and no longer working in the background. To bring it back to life type fg job_number

PP.
  • 10,764
  • 7
  • 45
  • 59
  • 1
    So...how do you exactly make it work in the background? How do I run my server applications in the background so that they do actually work without blocking me in terminal? – user1255410 Jul 12 '13 at 18:41
  • 9
    If a background process attempts to read from the terminal it is running on, it will be stopped, so that you can foreground it and provide the input it wants. If you want it to nave no input, then use `example < /dev/null &`. – twalberg Jul 12 '13 at 19:01
  • 2
    @twalberg you should make that a separate answer as it is the one that should be accepted as correct. – PP. Jul 12 '13 at 20:07
  • 1
    @user1255410 check out http://www.minecraftforum.net/forums/archive/legacy-support/1844264-server-wont-run-in-background - he's talking about the minecraft server cli but it generalizes to everything – Chuck Dries May 20 '16 at 03:59
1

From what I can gather.

Background jobs are blocked from reading the user's terminal. When one tries to do so it will be suspended until the user brings it to the foreground and provides some input. "reading from the user's terminal" can mean either directly trying to read from the terminal or changing terminal settings.

Normally that is what you want, but sometimes programs read from the terminal and/or change terminal settings not because they need user input to continue but because they want to check if the user is trying to provide input.

http://curiousthing.org/sigttin-sigttou-deep-dive-linux has the gory technical details.

plugwash
  • 9,724
  • 2
  • 38
  • 51
0

Just enter fg which will resolve the error when you then try to exit.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129