1

What a process group is, has been already asked and answered here:

A process group is a collection of related processes which can all be signalled at once.

Unfortunately there's no real world example of how and when you start a "a collection of related processes which can all be signalled at once".

Could anyone here give me some code example of where he's starting a process group and killing all the processes inside with kill -- -0?

For example would the apache webserver with it's children be a process group where when the main process is killed the rest of the PIDs within the process group are killed too?

2 Answers 2

2

There are many real world examples; some_app from the linked question could be ls or cat or the more complicated example

$ ps axo ppid,pid,pgid,command | grep $$
41613  2926  2926 mksh -l
 2926 14131 14131 ps axo ppid,pid,pgid,command
 2926 14132 14131 grep 2926
$ 

which shows that the shell (mksh) has started a new process group (14131) that has members 14131 and 14132. These could be signalled if not for the fact they do not linger around; for that you need a sleep or some form of blocking

$ perl -e 'warn $$; sleep 999' | perl -ne 'sleep 999'
14148 at -e line 1.

and then elsewhere you can run kill -- -14148 to kill that group.

1
  • Thanks, that's what I was looking for. One learns best in practice, not in theory.
    – manifestor
    Commented Dec 13, 2017 at 19:00
0

Example:

$ ( sleep 100 & sleep 100 & sleep 100 & )
$ ps -Ao pid,pgid,command | grep sleep
19734 19733 sleep 100
19735 19733 sleep 100
19736 19733 sleep 100
19789 19788 grep sleep

() creates a new subshell (at least in Bash), which will be the process group leader of all of the processes started (IOW: shell will create a new subshell, which PID will be the PGID of all three sleeps, which will be executed asynchronously in the background).

To kill those sleeps one can just type kill -19733.

Another example:

$ cat script1.sh 
sleep 100 &
./script2.sh

$ cat script2.sh 
sleep 100 &

$ ./script1.sh 
$ ps -Ao pid,pgid,command | grep sleep
21605 21604 sleep 100
21607 21604 sleep 100
21618 21617 grep sleep

or sleep 10 | sleep 20 | sleep 30 | sleep 40 & will also share the same PGID.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .