help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] How to kill all children oa script


From: Dan Douglas
Subject: Re: [Help-bash] How to kill all children oa script
Date: Fri, 13 Mar 2015 01:23:21 -0500

Hi Eduardo, the APUE chapter on process groups says this:

    "A process can set the process group ID of only itself or any of its
    children.  Furthermore, it can’t change the process group ID of one of its
    children after that child has called one of the exec functions.

    In most job-control shells, this function [setpgid] is called after a fork
    to have the parent set the process group ID of the child, and to have the
    child set its own process group ID.  One of these calls is redundant, but
    by doing both, we are guaranteed that the child is placed into its own
    process group before either process assumes that this has happened."

I take from this that the process group leader can't maintain a tree of job
control contexts because it's only responsible for setting the pgid of direct
descendents. A subshell that enables job control must become the leader of a
new process group.

     $ bash -O expand_aliases +O lastpipe -c 'echo; alias s="sleep 1"
    s | { s | s; } | pstree -cg $$'

    bash(20309)─┬─bash(20309)─┬─sleep(20309)
                │             └─sleep(20309)
                ├─pstree(20309)
                └─sleep(20309)

     $ bash -O expand_aliases +O lastpipe -c 'echo; alias s="sleep 1"
    s | { set -m; s | s; } | pstree -cg $$'

    bash(20338)─┬─bash(20338)─┬─sleep(20342)
                │             └─sleep(20342)
                ├─pstree(20338)
                └─sleep(20338)

Probably the only real reliable way to keep track of all descendents of a
process on Linux is to run the parent in a cgroup. Then you can search for all
members of a group and signal them.

My main reason for wanting to do this is testing. If that's your situation you
might prefer something like firejail or mbox. I like pflask personally.
https://github.com/ghedo/pflask
https://l3net.wordpress.com/projects/firejail/

--
Dan Douglas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]