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:
- 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 &
. - Exit the terminal after starting the program, which will cause the association with the program’s
stdin
to go away. But this will cause aSIGHUP
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 usingnohup
– 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 &