From: "Carlo E. Prelz" Date: 2013-01-25T02:56:10+09:00 Subject: Re: Processes and forks Subject: Processes and forks Date: Fri 25 Jan 13 02:04:38AM +0900 Sorry for the delay! Quoting baq s. (lists@ruby-forum.com): > Hi all, I have a quick problem that I am sure someone here has an > elegant solution for. > > I have a ruby script that runs in a loop an external linux process via > gdb: > > ... > until checks.empty? do > ... > begin > Timeout::timeout(5) { > system("/usr/bin/gdb --args #{proc} 2>/dev/null")} > rescue Timeout::Error > end > ... > end > ... > > What I am trying to do is have gdb terminate after 5 seconds of activity > and move on to debugging the next task. What I am finding however is > that after 5 seconds, my ruby script quits and returns to the command > line and the gdb and debugged processes are left running in the > background. > > I can then hit fg and it will start again but this is not what I want; I > want my script to execute gdb and launch it to debug a program, after 5 > seconds to terminate debugging and move on to the next binary to > debug. The biggest reason why your process does not die is most probably because you use system (which starts a shell). The signal reaches the shell but not gdb. I believe that gdb has a bit special way to handle interrupts. So, instead of system, you should use Process::spawn. But even then, read carefully the doc: if you pass a command line (like in your example), you will still have a shell process in the middle. To avoid that, you must pass the parts of the command in an array. Every time I do this, I have to spend a decent amount of time finding how to correctly split the command. Anyway, you may try with something like: pid=Process::spawn(array) begin Timeout::timeout(5) do Process::waitpid(pid) end rescue Timeout::Error Process::kill(pid,'KILL') Process::waitpid(pid) end You will have to experiment... Carlo -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)