Killing sons (Linux)

Discussion in 'Ruby' started by Ohad Lutzky, May 28, 2007.

  1. Ohad Lutzky

    Ohad Lutzky Guest

    Maybe this isn't strictly a Ruby question, but I hope someone here can
    help:

    I have a job-management application, with a central daemon which
    receives job requests. Upon receiving this request, it forks and then
    runs "system" to run bash, which in turn runs the Matlab job. I use bash
    for this in order to redirect the input and output from Matlab. pstree
    output looks like this:

    init-+-apache2---8*[apache2]
    |-atd
    ...
    |-ruby-+-4*[ruby---bash---MATLAB-+-matlab_helper]
    | | `-15*[{MATLAB}]]
    | `-{ruby}
    ...

    Legend:
    daemon ^ ^ daemon fork

    Now, my system also allows a 'kill' command, intended to stop the job in
    progress. This has been causing me a lot of trouble, and I suddenly
    (after quite a while the system has been in production, how embarassing)
    realized why - the PID I'm keeping is of the daemon fork. Killing it
    doesn't kill all of its sons - it causes bash to get reparented to init!

    Any idea of a clean, quick way to fix this?

    --
    Posted via http://www.ruby-forum.com/.
     
    Ohad Lutzky, May 28, 2007
    #1
    1. Advertising

  2. Ohad Lutzky

    Ohad Lutzky Guest

    Francis Cianfrocca wrote:
    > On 5/28/07, Ohad Lutzky <> wrote:
    >>
    >>
    >> Now, my system also allows a 'kill' command, intended to stop the job in
    >> progress. This has been causing me a lot of trouble, and I suddenly
    >> (after quite a while the system has been in production, how embarassing)
    >> realized why - the PID I'm keeping is of the daemon fork. Killing it
    >> doesn't kill all of its sons - it causes bash to get reparented to init!
    >>
    >> Any idea of a clean, quick way to fix this?

    >
    >
    >
    > Make your parent process the leader of its own process group with
    > setpgid(0,0). When you fork, add each child to the parent's process
    > group
    > with setpgid(0, getppid()). If you fork subchildren, make sure they get
    > added to the same process group. Now, to send a signal to the whole
    > group,
    > send it to (0 - pid), where pid is that of the parent. If you want them
    > all
    > to die without killing the leader, use a signal whose default behavior
    > is
    > terminate-process and ignore it in the parent.


    Just to be sure - if I run the following Ruby code on a Linux system:

    child = fork do
    Process::setpgid 0,0
    system 'bash -c "sleep 300"'
    end
    Process::kill 9, -child

    Then I am guaranteed that no child bash, sleep or ruby process will
    remain? It works, I just want to be sure I can count on that behaviour.
    For contrast, in my original code, bash gets reparented to init:

    child = fork do
    system 'bash -c "sleep 300"'
    end
    Process::kill 9, child

    And this code doesn't even work (ESRCH: No such process)

    child = fork do
    system 'bash -c "sleep 300"'
    end
    Process::kill 9, -child

    Thank you for your help!

    --
    Posted via http://www.ruby-forum.com/.
     
    Ohad Lutzky, May 28, 2007
    #2
    1. Advertising

  3. Ohad Lutzky

    Ohad Lutzky Guest

    I've learned how to do redirection from within ruby
    ($stdwhatever.reopen), and switched to using exec instead of system - so
    now I avoid two levels of depth. I also use setpgid and kill the whole
    group for good measure.

    Much thanks for your help!

    --
    Posted via http://www.ruby-forum.com/.
     
    Ohad Lutzky, May 30, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade

    Killing off my sessions

    Rob Meade, Feb 2, 2004, in forum: ASP .Net
    Replies:
    14
    Views:
    2,057
    Rob Meade
    Feb 4, 2004
  2. Michael Johnson Sr.

    Process killing

    Michael Johnson Sr., Feb 10, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    351
    Michael Johnson Sr.
    Feb 10, 2004
  3. =?Utf-8?B?Q2FybG9z?=

    Many ASP.NET controls killing my CPU

    =?Utf-8?B?Q2FybG9z?=, Apr 8, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    644
    =?Utf-8?B?Q2FybG9z?=
    Apr 12, 2004
  4. moondaddy
    Replies:
    3
    Views:
    8,147
    Steven Cheng[MSFT]
    Apr 21, 2004
  5. Jerome

    asp.net killing foreign letters??

    Jerome, Apr 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    443
    Jerome
    Apr 23, 2004
Loading...

Share This Page