I like my background processes to freely write to the tty. stty -tostop is already the default in my zsh (I don't know why, perhaps because of OhMyzsh?):
❯ stty -a |rg tostop
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
But I still occasionally get my background processes suspended (this is not a consistent behavior, and I don't know how to reproduce it):
[1] + 3479064 suspended (tty output)
trap -- '' SIGTTOUwork? – HappyFace Jul 26 '20 at 20:15trap -- '' TTOU SIGTTOU. It doesn't work, the process will still get suspended. – HappyFace Jul 26 '20 at 20:19cmd < /dev/null. What's the command that is causing you trouble? – Stéphane Chazelas Jul 26 '20 at 20:23trapusing your own example ofstty echo.) The setup is somewhat complex, and I have only once encountered this suspension error in it, though the error comes up in other contexts every few months. In that last time, a foregroundsleep 30was suspended, while there was another script running in the background, which seemed to have been killed (with no messages). I didn't understand the situation at all. I am hoping that I can sweep the problem under the rug by just ignoring SIGTTOU, as I don't care who does what with the TTY. – HappyFace Jul 26 '20 at 20:37stty -echoctl, it would then always suspend when launched into background... &. I searched for and tried many tips (different shell, redirecting output e.g.... &>/dev/nullor... | tee log, settingstty -tostopetc.), none worked. What finally fixed it was to add</dev/nullto when launching the script (even though the script takes no console input) as suggested by @StéphaneChazelas in a comment above. Once the stdin was redirected, the script could be launched in the background again. – Petr Vepřek Apr 01 '23 at 20:34