[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Error Exception if script doesn't run

jackster the jackle

10/17/2008 3:29:00 PM

I need to know if my script fails to run for any reason and capture the
exception. I know how to use "rescue" for small pieces of code but how
can I capture the stderr for anything in the script?

thanks

jack
--
Posted via http://www.ruby-....

19 Answers

Brian Candler

10/17/2008 3:40:00 PM

0

jackster the jackle wrote:
> I need to know if my script fails to run for any reason and capture the
> exception. I know how to use "rescue" for small pieces of code but how
> can I capture the stderr for anything in the script?

If you want to capture only an *exception* then wrap it like this:

begin

.. rest of script

rescue Exception => e
.. do something with e, e.g. write it to a log file
end

However if you want to capture everything written to stderr then that's
a different problem. That's the responsibility of whoever started the
Ruby process, since it would have passed it an open file descriptor for
stderr (fd 2). For example, if you are starting ruby from a shell, then

$ ruby foo.rb 2>errors.log

But if you were launching it from another Ruby process, then look at
open3.rb in the standard library.
--
Posted via http://www.ruby-....

jackster the jackle

10/17/2008 5:11:00 PM

0

that does exactly what I had hoped....thanks alot!

jack
--
Posted via http://www.ruby-....

jackster the jackle

10/17/2008 9:46:00 PM

0

I notice that any shell commands that I have in the script that fail do
not trigger a Ruby exception, how do I get my Ruby script to send these?

thanks

jack





Brian Candler wrote:
> jackster the jackle wrote:
>> I need to know if my script fails to run for any reason and capture the
>> exception. I know how to use "rescue" for small pieces of code but how
>> can I capture the stderr for anything in the script?
>
> If you want to capture only an *exception* then wrap it like this:
>
> begin
>
> .. rest of script
>
> rescue Exception => e
> .. do something with e, e.g. write it to a log file
> end
>
> However if you want to capture everything written to stderr then that's
> a different problem. That's the responsibility of whoever started the
> Ruby process, since it would have passed it an open file descriptor for
> stderr (fd 2). For example, if you are starting ruby from a shell, then
>
> $ ruby foo.rb 2>errors.log
>
> But if you were launching it from another Ruby process, then look at
> open3.rb in the standard library.

--
Posted via http://www.ruby-....

Joel VanderWerf

10/17/2008 10:26:00 PM

0

jackster the jackle wrote:
> I notice that any shell commands that I have in the script that fail do
> not trigger a Ruby exception, how do I get my Ruby script to send these?

Check the return value of #system, like this:

irb(main):002:0> unless system "false"; puts "failed!"; end
failed!
=> nil

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

jackster the jackle

10/17/2008 11:11:00 PM

0

It seems to work but not for this command, perhaps since scp gets
executed (but does not complete) the system variable is not false?

[code]
`scp -i /home/jsmith/.ssh/id_rsa /files*.tar jsmith@172.20.1.1:`
[/code]

The error code I get when I run the script manually is as follows
because 172.20.1.1 is no longer on the network:

ssh: connect to host 172.20.1.1 port 22: No route to host
lost connection

I was hoping the system variable would trip when this command fails?

jack



Joel VanderWerf wrote:
> jackster the jackle wrote:
>> I notice that any shell commands that I have in the script that fail do
>> not trigger a Ruby exception, how do I get my Ruby script to send these?
>
> Check the return value of #system, like this:
>
> irb(main):002:0> unless system "false"; puts "failed!"; end
> failed!
> => nil

--
Posted via http://www.ruby-....

Tim Hunter

10/18/2008

0

jackster the jackle wrote:
> It seems to work but not for this command, perhaps since scp gets
> executed (but does not complete) the system variable is not false?
>
> [code]
> `scp -i /home/jsmith/.ssh/id_rsa /files*.tar jsmith@172.20.1.1:`
> [/code]
>
> The error code I get when I run the script manually is as follows
> because 172.20.1.1 is no longer on the network:
>
> ssh: connect to host 172.20.1.1 port 22: No route to host
> lost connection
>
> I was hoping the system variable would trip when this command fails?
>
> jack

In the general case, when a command succeeds it returns 0 and when it
fails it returns something else. However, this is a convention, not a
rule. It is entirely possible for a command to fail and still return 0.
If so, you can't determine if it fails by simply checking the return
code. I have no idea whether or not scp returns non-0 on failure. You
can check by running it yourself in a shell. Immediately after it fails
enter 'echo $?'. This will show you the exit status. For example:

~$ ls test.rb
test.rb
~$ echo $?
0
~$ ls nothere
ls: nothere: No such file or directory
~$ echo $?
1

If your scp command returns 0 in this case then the only way to detect
that it failed is to inspect the message, which it probably is writing
to stderr.

More information about exit status and $? can be found on the Internet.

--
RMagick: http://rmagick.ruby...

jackster the jackle

10/18/2008 12:49:00 PM

0

Tim Hunter wrote:
> ~$ ls test.rb
> test.rb
> ~$ echo $?
> 0
> ~$ ls nothere
> ls: nothere: No such file or directory
> ~$ echo $?
> 1
>
Based on what you have described, I think I have figured out a way to
make it work...but I still have one question.

When the following command completes successfully:
y= `cp /home/jsmith/js.txt /home/jsmith/js2.txt`
puts $?

$? equals 0, however if I change the path to js.txt to something that
doesn't exist in order to make it fail, $? has a value of 256.

I thought $? should always have a value of 1 when it fails? Why is it
256?

thanks
jack
--
Posted via http://www.ruby-....

Henrik Nyh

10/18/2008 1:02:00 PM

0

On Sat, Oct 18, 2008 at 2:48 PM, jackster the jackle
<johnsheahan@sflistdb.com> wrote:
> Tim Hunter wrote:
>> ~$ ls test.rb
>> test.rb
>> ~$ echo $?
>> 0
>> ~$ ls nothere
>> ls: nothere: No such file or directory
>> ~$ echo $?
>> 1
>>
> Based on what you have described, I think I have figured out a way to
> make it work...but I still have one question.
>
> When the following command completes successfully:
> y= `cp /home/jsmith/js.txt /home/jsmith/js2.txt`
> puts $?
>
> $? equals 0, however if I change the path to js.txt to something that
> doesn't exist in order to make it fail, $? has a value of 256.
>
> I thought $? should always have a value of 1 when it fails? Why is it
> 256?

It is an error code, so it's sometimes other values than 1. Success is always 0.

David Rio

10/18/2008 9:21:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Fri, Oct 17, 2008 at 6:11 PM, jackster the jackle <
johnsheahan@sflistdb.com> wrote:

> It seems to work but not for this command, perhaps since scp gets
> executed (but does not complete) the system variable is not false?
>
> [code]
> `scp -i /home/jsmith/.ssh/id_rsa /files*.tar jsmith@172.20.1.1:`
> [/code]
>

I would recommend using rsync instead of scp.
You could do first an rsync and then perform a --dry-run to confirm that
your
data has been transferred correctly.

-drd

Brian Candler

10/19/2008 8:28:00 AM

0

jackster the jackle wrote:
> $? equals 0, however if I change the path to js.txt to something that
> doesn't exist in order to make it fail, $? has a value of 256.

That's because the low-order 8 bits of $? encode something different;
the next 7 bits are the exit code (0 to 127). Use $?.exitstatus to get
that.

http://www.ruby-doc.org/core/classes/Process/S...

> I thought $? should always have a value of 1 when it fails? Why is it
> 256?

The exit status in this case is 256 >> 8, which is 1.
--
Posted via http://www.ruby-....