Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

Killing sons (Linux)


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/.

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/.

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/.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc