什么可以用来在 Bourne Shell(或 csh)中生成完全独立的进程
我正在测试一种与 sh 中的原始进程分开运行进程的方法。 我已经问过并听说过&用于生成子进程。 通过使用反引号调用程序(在本例中为 glxgears),因为它有很多 STDOUT(因此我可以测试输出的去向)。
glxgears&
不会将输出保留在当前 shell 之外,这
`glxgears`&
导致
comp:~ user$ `glxgears`&
[1] 14511
comp:~ user$ X connection to /tmp/launch-dZalNv/org.x:0 broken (explicit kill or server shutdown).
[1]+ Exit 1 `glxgears`
comp:~ user$
我接受 [1] 14511
只是正确进程启动的通知,但 X Server 仍然如何能够将输出转储到我的 shell 中吗? 为什么如果我启动了一个单独的进程正在
[1]+ Exit 1 `glxgears`
显示? shell 永远不会警告我其他关闭进程!
我想我已经推断出这可能与进程组有关,即使在我的 shell 关闭后,该进程组仍然是我的 shell 的 PID。所有其他进程都有自己的进程组。 甚至使用
`glxgears&`&
保留了原来shell的PID作为进程组!
我希望能够运行一个程序(在本例中为 glxgears),而与启动 shell 没有任何关联,没有任何类型的输出。
如果您还可以解释为什么 X 服务器可以向我的启动进程发送输出,我们将非常感激。
I'm testing out a way to run a process separate from the original process in sh.
I've already asked and heard that & is used for spawning a child process.
By calling a program, in this case glxgears, because it has a lot of STDOUT (so I can test where output goes), using backticks.
glxgears&
does not keep output out of the current shell where
`glxgears`&
causes
comp:~ user$ `glxgears`&
[1] 14511
comp:~ user$ X connection to /tmp/launch-dZalNv/org.x:0 broken (explicit kill or server shutdown).
[1]+ Exit 1 `glxgears`
comp:~ user$
I accept that [1] 14511
is just notification of proper process launch, but how is the X Server still able to dump output in my shell?
And why if I have launched a separate process is
[1]+ Exit 1 `glxgears`
Showing up? The shell never warns me of other closing processes!
I think I've deduced that it may have to do with the process group which remains the PID of my shell even after my shell is closed. All other processes have their own process group.
Even using
`glxgears&`&
retains the original shell's PID as the process group!
I want to be able to run a program (in this case glxgears) without any association to the launching shell, no output of any kind.
If you could also explain why the X server can send my starting process the output that be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
2>&1
使程序的 stderr 成为 stdout。>/dev/null
使 stdout 成为 /dev/null,这样你在终端上就看不到任何内容,并且如果你关闭终端,程序也不会收到破坏管道信号nohup
,如果其父程序终止,程序将不会终止Try this:
2>&1
makes the program's stderr to be stdout.>/dev/null
makes stdout to be /dev/null, so that you will see nothing on your terminal, and the program will not receive a boken pipe signal if you close your terminalnohup
, the program will not terminate if its parent is terminated您似乎混淆了 I/O 流的进程关联。后台进程的唯一特别之处是它们将交互控制返回给调用 shell,仅此而已。它们仍然使用同一终端的相同 stdout 和 stderr 流。他们还会将输出写入哪里?唯一要做的半合理的事情是(a)完全丢弃输出(b)将它们指向一些新文件。两者都被认为不太可能成为默认行为,因此默认情况下仍然使用调用进程的终端进行输出。您可以使用
>
运算符覆盖此选择,如 arnaud 所示。You seem to be confusing process association with I/O streams. The only thing special about background processes is that they return interactive control to the calling shell, nothing more. They still use the same stdout and stderr streams of the same terminal. Where else would they write output to? The only semi-reasonable things to do would be (a) to discard output completely (b) to point them to some new file. Both were considered too unlikely to be useful to be the default behavior, so the default is still to use the terminal of the calling process for output. You can override this choice with the
>
operator, as arnaud has shown.