Created
July 20, 2011 12:28
-
-
Save macarthy/1094866 to your computer and use it in GitHub Desktop.
Experimenting with forking, and monitoring processes in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby -w | |
# from : https://www.ruby-forum.com/topic/135350 | |
# SIG_CHILD to monitor processes | |
trap_quit = lambda { |sig| | |
signame = Signal.list.find {|k, v| v == sig }[0] | |
STDERR.puts("Trapping signal #{signame}") | |
} | |
huphandler = inthandler = termhandler = chldhandler = nil | |
huphandler = trap("HUP", &trap_quit) | |
inthandler = trap("INT", &trap_quit) | |
termhandler = trap("TERM", &trap_quit) | |
cmd_exitstatus = nil | |
chldhandler = trap("CHLD") { | |
STDERR.puts("Trapping signal CHLD") | |
begin | |
# tmppid, status = Process.wait2(cmdpid) | |
tmppid = Process.wait | |
status = $? | |
cmd_exitstatus = status.exitstatus | |
STDERR.puts("Process::Status => " + status.inspect) | |
STDERR.puts("exitstatus => " + cmd_exitstatus.inspect) | |
if ! status.exited? | |
raise "ARGH! The child has not exited yet!" | |
end | |
rescue Errno::ECHILD => e | |
STDERR.puts("Command failed - probably ok? #{e.inspect}") | |
# bah, ignore it, just means a command failed | |
end | |
} | |
cmdpid = fork { | |
begin | |
exec("dd", "if=/dev/zero", "of=/tmp/zerofile1.asdfsdf", | |
"bs=1", "count=333000000") | |
rescue Errno::ENOENT => e | |
STDERR.puts e.message | |
exit 127 | |
end | |
} | |
while(cmd_exitstatus.nil?) | |
sleep 1 | |
STDOUT.puts "still going..." | |
STDOUT.flush | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment