Prev: change gem user agent
Next: awesome-cli-ruby
From: Martin Hansen on 18 Jun 2010 05:34 Hello, I would like to, for a given script, to print the exit status - such as normal, interrupted, terminated, etc. to a log file. How can I do that? Martin -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 18 Jun 2010 12:08 2010/6/18 Martin Hansen <mail(a)maasha.dk>: > I would like to, for a given script, to print the exit status - such as > normal, interrupted, terminated, etc. to a log file. How can I do that? Not clear what you intend to do. Is the script a Ruby script? Then you do it with any process. Do you want to print the status of another program in a Ruby script? Where do you want to print that? Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Martin Hansen on 21 Jun 2010 01:47 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. 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). The log file is just a file dedicated to this script. Cheers, Martin Robert Klemme wrote: > 2010/6/18 Martin Hansen <mail(a)maasha.dk>: >> I would like to, for a given script, to print the exit status - such as >> normal, interrupted, terminated, etc. to a log file. How can I do that? > > Not clear what you intend to do. Is the script a Ruby script? Then > you do it with any process. Do you want to print the status of > another program in a Ruby script? Where do you want to print that? > > Cheers > > robert -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 21 Jun 2010 05:02 Please don't top post. 2010/6/21 Martin Hansen <mail(a)maasha.dk>: > 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> $? => #<Process::Status: pid 712 exit 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" #<Process::Status: pid 3160 exit 2> 2 => [#<Process::Status: pid 3160 exit 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/
From: Martin Hansen on 21 Jun 2010 05:22
Hm, I am not making myself clear :o( - It is the status of the ruby script at hand I want to log. $? is probably barking up the wrong tree and adding confusion. In Perl I would do this trapping the signal. > 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. #!/usr/bin/env ruby signal = nil trap("INT") { signal = "interupted"; exit } #trap("TERM") { signal = "terminated"; exit } at_exit { puts "Put this in logfile: #{signal}" } sleep 5 I have two issues with this: I loose the stack trace that is normally printed to stderr (I still want that printed to stderr): /test.rb:10:in `sleep': Interrupt from ./test.rb:10:in `<main>' And I fail setting signal as a global variable (if that can be done?) so I can use it from within a class without getting errors like this: in `block in <class:Biopieces>': undefined local variable or method `signal' for Biopieces:Class (NameError) Cheers, Martin -- Posted via http://www.ruby-forum.com/. |