If you start a background program in the terminal with the & option. Will it stop when you close the terminal?
3 Answers
It stops when you hang up the terminal. Terminals don't necessarily have the concept of being closed.
With a terminal emulator program, there isn't anything real being hung up, but closing the emulator program triggers an emulated terminal hangup. (More strictly: closing the master side of the pseudo-terminal device qualifies as terminal hangup on the slave side.) Not all emulator programs have the notion of being closed. The one that is built into the operating system kernel, providing the kernel's virtual terminals, cannot be "closed", for example.
With a real terminal, there is a serial port with a carrier detect line, and a real modem hangup that can occur if the dial-in user on the remote terminal hangs up the telephone connection.
This all dates back to the days of timesharing systems with remote terminals where users were billed for use. If one accidentally lost one's telephone connection, one most certainly didn't want to be billed for programs still running, potentially until the next system restart. Conversely, system administrators and other users didn't want programs still running and attached to now notionally free terminals that might be then assigned to other dial-in users.
A lot has changed since then. BSD job control, for starters, inspired changes to the distribution of the hangup signal and the introduction of the notion of a session leader that is in charge of the jobs within a login session and whose death is considered to be a crushing blow to those jobs. We've gained remote login over the Internet, and the idea of a GUI login session. And terminal emulator programs have become so prevalent that people conflate them with the terminals that they are emulating.
But the fundamental idea remains that user processes are in the normal case terminated — somehow, even if through the mediation of a session leader — when their terminal session ceases.

- 3,959
Visit Running programs in the background from terminal or Most efficient way of launching and separating a program from the terminal?
If you start a background program in the terminal with the & option. Will it stop when you close the terminal?
I find closing gnome-terminal
by clicking on close button can cause stopping/killing of that program. But if you close terminal by exit
command than program will not close/stop/kill.
Yes, it will stop. To avoid the program being stopped, use nohup
.
For instance, nohup firefox > /dev/null 2>&1 &
, will both allow running firefox in background without closing and prevent error/output showing on console

- 105,154
- 20
- 279
- 497
-
2Or to achieve the same effect of
nohup <command> > /dev/null 2>&1 &
usebash
'sdisown
builtin:<command> > /dev/null 2>&1 & disown
or, frombash
4 onwards<command> &> /dev/null & disown
– kos Jun 12 '15 at 20:48
firefox &
if you want it to survive in case of exiting terminal you can usesetsid firefox
– kenn Jun 12 '15 at 20:07