Can anyone please advise, how to spawn a new process on linux by using new process group id in BASH unix shell? Thank you so much!
To run a command, eg "emacs xxx.txt", in a new process group, from a command prompt ie interactively:
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). 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 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:
setsid emacs xxx.txt
... blocks (see the linked article). But it does set a new process group before running emacs
you can background it with '&':
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)
ie when we double the setsid the sequence is:
From within a script, if you want to communicate with the command and wait for it to exit, see subprocess in the bash manpage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With