Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set process group of a shell script

How to set process group of a shell script ? Also I want all the child process to be in the same process group

I expect something similar to setpgid() in C.

like image 653
Jacob Avatar asked Jul 01 '11 14:07

Jacob


People also ask

What is group PID?

Sets the process group ID (PGID) of a process within the session of the calling process, so you can reassign a process to a different process group, or start a new process group with the specified process as its group leader. pid_t pid is the process ID (PID) of the process whose PGID you want to change.

What is Process Group in Linux?

Process Groups A process group in Linux, as the name suggests, is a way that Linux groups process together. They are a collection of related processes sharing the same PGID's (Process Group ID). A common misconception is that killing the parent process will kill the children's processes too.

How can I change my Pgid?

These calls are reserved to the process itself: a process cannot change another process's PGID or SID, with one exception: a process can change its children's PGID if they're still running the original process image (i.e. they haven't called execve to run a different program).


1 Answers

As PSkocik points out, it is possible to run a process in its own process group, in most shells, by activating job control (“monitor mode”).

(set -m; exec process_in_its_own_group) 

Linux has a setsid utility, which runs the command passed as argument in its own session (using the eponymous system call). This is stronger than running it in its own process group à la setpgrp, but that may be ok for your purpose.

If you want to place the process in an existing group rather than in its own group (i.e. if you want the full power of setpgid), there's no common shell utility. You have to use C/Perl/…

like image 52
Gilles 'SO- stop being evil' Avatar answered Sep 21 '22 06:09

Gilles 'SO- stop being evil'