If I background a processes in a script or a -c snippet, the backgrounded processes ignores SIGINT and SIGQUIT:
Example:
$ alias ps='ps -o pid,ppid,pgrp,sid,stat,tty,ignored,blocked,caught,wchan,min_flt,pmem,args --forest'
$ sh -c 'sleep 1000 & sleep 1000 | sleep 1000' & \
sleep 0.01; ps |grep -v -e ps -e grep
PID PPID PGRP SID STAT TT IGNORED BLOCKED CAUGHT WCHAN MINFL %MEM COMMAND
6197 2143 6197 6197 Ss pts/28 0000000000380004 0000000000010000 000000004b817efb wait 10039 0.0 -bash
7593 6197 7593 6197 S pts/28 0000000000000000 0000000000000000 0000000000010002 wait 148 0.0 \_ sh -c sleep 1000 & sleep 1000 | sleep 1000
7595 7593 7593 6197 S pts/28 0000000000000006 0000000000000000 0000000000000000 hrtime 85 0.0 | \_ sleep 1000
7596 7593 7593 6197 S pts/28 0000000000000000 0000000000000000 0000000000000000 hrtime 85 0.0 | \_ sleep 1000
7597 7593 7593 6197 S pts/28 0000000000000000 0000000000000000 0000000000000000 hrtime 85 0.0 | \_ sleep 1000
This means that if I run kill -INT -$!
(or fg
followed by Ctrl-C
) from the interactive parent shell (bash), the sleep processes backgrounded from the -c
snippet isn't reached and survives.
PID PPID PGRP SID STAT TT IGNORED BLOCKED CAUGHT WCHAN MINFL %MEM COMMAND
6197 2143 6197 6197 Ss pts/28 0000000000380004 0000000000010000 000000004b817efb wait 10103 0.0 -bash
7595 1 7593 6197 S pts/28 0000000000000006 0000000000000000 0000000000000000 hrtime 85 0.0 sleep 1000
What is the reason for this behavior? Can it be disabled?
ps
output. The process backgrounded from the -c snippet has the same process group number, however, SIGINT and SIGQUIT are included in its ignored signal mask (0000000000000006). – HeterocliteWhen job control is not in effect, asynchronous commands ignore SIGINT and SIGQUIT in addition to these inherited handlers.
– Falgout(trap '' INT QUIT; exec the_command)
) half-assed (Ctrl-Z
can still reach the background process) emulation of processes group, but whatever the reason for it is, I found it in POSIX (pubs.opengroup.org/onlinepubs/9699919799/utilities/…) so I guess I'll have to deal with it, no matter how annoying it is. – Heteroclitesleep 1000 & sleep 1000', especially to show the ignore effect is due to what
&` in a script does and not due to inheritance from the script's parent. The pipeline is clearly unnecessary in the example, but pipeline links are somewhat like backgrounding (in that they add pararelization to an otherwise conceptually singlethreaded script) so it doesn't hurt much, IMO, to throw it in there to show how signal dispositions are handled in a pipeline. – Heteroclite