Let's look more closely at what's happening to less
:
$ pdfgrep -R -i spark . | strace less &
[...]
open("/dev/tty", O_RDONLY|O_LARGEFILE) = 3
ioctl(3, TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
ioctl(3, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig -icanon -echo ...}) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGTTOU {si_signo=SIGTTOU, si_code=SI_KERNEL} ---
--- stopped by SIGTTOU ---
Job control restricts the processes in a background job from performing certain operations on the controlling terminal.
If a background process tries to read from the terminal, it will be sent a SIGTTIN
signal, which typically stops (pauses) the process.
If a background process tries to set a terminal's parameters, it will be sent a SIGTTOU
signal, which also typically stops the process. That's what is happening here with the TCSETSW
ioctl. The less
program tries to put the terminal into raw mode soon after it starts, even before it knows whether it has anything to display.
There is a good reason for this: you don't want a background job asynchronously changing your terminal so that, for example, raw mode is on and echo is off.
(A background process can get terminal parameters with the TCGETS
ioctl without being stopped - see the listing above.)
If a background process tries to write to the terminal and the terminal has the tostop
flag set, it will be sent the SIGTTOU
signal.
You probably don't have the tostop
flag set (run stty -a
to check). If you don't, a background command like pdfgrep -R -i spark . &
that doesn't change any terminal settings will be able to write to your terminal whenever it tries.
You also wrote:
The reason that I pipe to less is because I don't want the output to stdout messes up the screen of my terminal session when I am doing something else
The less program is ultimately going to send output to the terminal, one screenful at a time. If you run stty tostop
before pdfgrep | less &
, or before pdfgrep &
, then they will only output to your terminal when they are in the foreground.
less
reads from stdin only when I type some commands ofless
, such asq
. Is that what you mean? – Tim Oct 08 '17 at 14:33pdfgrep -R -i spark . > /dev/null 2>&1
I don't remember if the & for background goes before the first > or after the 1. I don't have Linux atm. This will send stderr to stdout (terminal by default), but then stdout to /dev/null, discarding it – JorgeeFG Oct 09 '17 at 13:56screen
to have background sessions – JorgeeFG Oct 09 '17 at 14:13