Linux process in background - “Stopped” in jobs?

后端 未结 4 557
北海茫月
北海茫月 2020-12-13 09:04

I\'m currently running a process with the & sign.

$ example &

However, (please note i\'m a newbie to Linux) I realised

相关标签:
4条回答
  • 2020-12-13 09:46

    Yes it really is stopped and no longer working in the background. To bring it back to life type fg job_number

    0 讨论(0)
  • 2020-12-13 09:52

    Just enter fg which will resolve the error when you then try to exit.

    0 讨论(0)
  • 2020-12-13 09:58

    From what I can gather.

    Background jobs are blocked from reading the user's terminal. When one tries to do so it will be suspended until the user brings it to the foreground and provides some input. "reading from the user's terminal" can mean either directly trying to read from the terminal or changing terminal settings.

    Normally that is what you want, but sometimes programs read from the terminal and/or change terminal settings not because they need user input to continue but because they want to check if the user is trying to provide input.

    http://curiousthing.org/sigttin-sigttou-deep-dive-linux has the gory technical details.

    0 讨论(0)
  • 2020-12-13 10:03

    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:

    1. 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 &.
    2. Exit the terminal after starting the program, which will cause the association with the program's stdin to go away. But this will cause a SIGHUP 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 using nohup - 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 &
    
    0 讨论(0)
提交回复
热议问题