From: Robert Klemme Date: 2010-06-21T18:02:20+09:00 Subject: Re: How to print exit status to a log file? Please don't top post. 2010/6/21 Martin Hansen : > I would like to print the exit status from within the current Ruby > script to a log file. I have been messing a bit with $?, but that way I > get "pid 8144 exit 0" in the log file and "Interrupted" goes to terminal > - I wanted "Interrupted" in the log file. "Interrupted" is not an exit status. This is most likely written by the other process that you started. You either need to catch the other process's output (most likely stderr) or invent a mechanism to report the interruption via the exit status (this works only if you have control over the other process's code). Then you can interpret the exit status to write your log message. irb(main):008:0> system "true" => true irb(main):009:0> $? => # irb(main):010:0> $?.exitstatus => 0 If you want to catch the output you can use one of the popen methods, e.g. irb(main):001:0> require 'open3' => true irb(main):002:0> Open3.popen3 "ls", "nonexistent" do |si,so,serr,th| irb(main):003:1* p serr.read irb(main):004:1> st = th.value irb(main):005:1> p st, st.exitstatus irb(main):006:1> end "ls: cannot access nonexistent: No such file or directory\n" # 2 => [#, 2] > I have not been able to make > trap() do the trick, and with trap I loose the default exit and stack > trace output (I think trap is to be avoided). trap in your Ruby script will trap signals for this process not the forked process - unless you use the block form of fork in which case I believe the child inherits all signal handlers. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/