后台的 Linux 进程 - 作业中的“停止”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17621798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:15:06  来源:igfitidea点击:

Linux process in background - "Stopped" in jobs?

linuxbash

提问by user1255410

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

我目前正在运行带有&标志的进程。

$ example &

However, (please note i'm a newbie to Linux) I realised that pretty much a second after such command I'm getting a note that my process received a stopped signal. If I do

但是,(请注意,我是 Linux 的新手)我意识到在执行这样的命令后几乎一秒钟我就会收到一条消息,表明我的进程收到了停止信号。如果我做

$ jobs

I'll get the list with my example process with a little note "Stopped". Is it really stopped and not working at all in the background? How does it exactly work? I'm getting mixed info from the Internet.

我将通过我的示例流程获取列表,并附上一个小注释“已停止”。它真的停止并且在后台根本不工作吗?它是如何工作的?我从互联网上得到了混合信息。

采纳答案by twalberg

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 SIGTTINsignal, which by default causes the program to be completely stopped, pending the user bringing it to the foreground (fg %jobor similar) to allow input to actually be given to the program. To avoid the program being paused in this way, you can either:

在Linux和其他Unix系统中,在后台运行的,但仍然有一个工作的stdin(或std::cin)用其控制终端相关联的(又名它是在运行窗口)将被发送的SIGTTIN信号,其由缺省导致程序完全停止,等待用户将其带到前台(fg %job或类似)以允许实际向程序提供输入。为避免程序以这种方式暂停,您可以:

  1. Make sure the programs stdinchannel 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/nullif 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 stdinto go away. But this will cause a SIGHUPto 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 &.
  1. 确保程序stdin通道不再与终端相关联,方法是将其重定向到具有适当内容的文件以供程序输入,或者/dev/null如果它确实不需要输入 - 例如myprogram < /dev/null &.
  2. 启动程序后退出终端,这将导致与程序的关联stdin消失。但这会导致将 aSIGHUP传递给程序(意味着输入/输出通道经历了“挂断”)-这通常会导致程序终止,但这可以通过使用nohup-例如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 &

回答by PP.

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

是的,它真的停止了,不再在后台工作。让它恢复生机,输入fg job_number

回答by plugwash

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-linuxhas the gory technical details.

http://curiousthing.org/sigttin-sigttou-deep-dive-linux有血腥的技术细节。

回答by SimpleSimon

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

只需输入即可fg在您尝试退出时解决错误。