简体   繁体   English

如何使用BASH中的新进程组ID在linux上生成进程

[英]How to spawn process on linux by using new process group id in BASH

Can anyone please advise, how to spawn a new process on linux by using new process group id in BASH unix shell? 任何人都可以建议,如何在BASH unix shell中使用新进程组ID在linux上生成一个新进程? Thank you so much! 非常感谢!

To run a command, eg "emacs xxx.txt", in a new process group, from a command prompt ie interactively: 要在新进程组中从命令提示符运行命令,例如“emacs xxx.txt”,即以交互方式运行:

setsid emacs xxx.txt &

As described in the linked "linux command setsid", the setsid command behaviour can be surprising (and in the linux I'm using just now the manpage is not helpful at all). 如链接的“linux命令setsid”中所述,setsid命令行为可能令人惊讶(在我刚才使用的linux中,联机帮助页根本没用)。 So... 所以...

If you want to spawn a command from within a script and have the command continue after the script exits , double the setsid: 如果要从脚本中生成命令并在脚本退出后继续执行命令,请将setsid加倍:

setsid setsid emacs xxx.txt

... the rationale is: ......理由是:

  • when bash invokes setsid during script, setsid keeps the process group id of the bash interpreter process, and so: 当bash在脚本期间调用setsid时,setsid会保留bash解释器进程的进程组ID,因此:

    setsid emacs xxx.txt setsid emacs xxx.txt

    ... blocks (see the linked article). ...块(参见链接文章)。 But it does set a new process group before running emacs 但它确实在运行emacs之前设置了一个新的进程组

  • you can background it with '&': 你可以用'&'来背景:

    setsid emacs xxx.txt & setsid emacs xxx.txt&

    ... but that leaves a race condition where the calling script might exit before the background process gets to the point where setsid changes the process group (which might kill the sub process) ...但是这会留下一个竞争条件,其中调用脚本可能会在后台进程到达setsid更改进程组(可能会终止子进程)之前退出

  • so we want to wait for the process group change to happen; 所以我们要等待进程组的更改发生; the nested setsid is guaranteed to fork because the outer setsid is not a process group leader, and will already have a new process group id set by the outer setsid 保证嵌套的setsid是fork,因为外部的setsid不是进程组的负责人,并且已经有外部setsid设置的新进程组ID

ie when we double the setsid the sequence is: 即当我们将setsid加倍时,序列是:

  1. bash forks child 1, leaving it in bash's process group bash forks child 1,将它留在bash的进程组中
  2. child 1 execs setsid 孩子1个高管
  3. child 1 setsid sees it is not a group leader and so calls setsid system call (which creates new session and gives child 1 a new process group) and then execs inner setsid child 1 setsid看到它不是组长,因此调用setsid系统调用(创建新会话并为子1提供一个新进程组)然后执行内部setsid
  4. inner setsid sees it is a group leader, so forks child 2, child 1 exits 内部setid看到它是一个组长,所以分叉子2,孩子1退出
  5. child 2 sees it is not a group leader so calls setsid system call (which creates a new session and gives child 2 a new process group) and then execs emacs 子2看到它不是组长,所以调用setsid系统调用(创建一个新的会话并给孩子2一个新的进程组)然后执行emacs

From within a script , if you want to communicate with the command and wait for it to exit , see subprocess in the bash manpage. 在脚本中 ,如果要与命令通信并等待它退出 ,请参阅bash联机帮助页中的subprocess。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM