Sometimes, some time after I’ve backgrounded a process with bg in bash, when I press Enter in the same shell to redisplay the prompt (just to check that I’m still in bash when some output from the background process has been displayed), the background process seems to stop spontaneously.
If I do bg again the same problem recurs.
The only way to fix it seems to be fg.
Why does this happen?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This usually happens if the process tries to read from its stdin stream. When the process is in the background, it receives a TTIN signal and is thus frozen (same behavior as a STOP signal). There is also the dual signal TTOU when a background process tries to write to its terminal.
Bringing it to the foreground resumes the process and allows it to read from your terminal.
Demo:
$ cat t.sh #! /bin/sh sleep 1 read dummy $ ./t.sh & [1] 3364 $ [1]+ Stopped ./t.sh $ ps aux|grep t.sh me 3364 0.0 0.0 11268 1200 pts/0 T 17:04 0:00 /bin/sh ./t.sh
One of the ways of avoiding this is to use nohup, but this can have strange effects if the program doesn’t deal with having its input stream redirected to /dev/null.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0