tags:

views:

41

answers:

2

Hello,

I'm trying to manage a ssh connection to a network device via the PTY module, with a code similar to this :

cmd_line = "ssh [email protected]"
begin
  PTY.spawn(cmd_line) do |r_f,w_f,pid|
  ...
rescue PTY::ChildExited => cended
  ...
end

The whole i/o works pretty well, however I don't know how to get the exit status of the child process !

For instance if the connection is broken or simply times out, the spawned process will terminate (with an error code) but this code does not seem to be returned in the $? special variable.

If you have any ideas on how to get the spawned process return code, I'd really appreciate !

Regards,

_dl

A: 

In abstract: In Linux The parent should wait()s for this child to know the exit status of his child.
C code:

int status;
wait(&status) // in the parent code part
WEXITSTATUS(status) //macro to return the exit code of the returned child

I'm sorry.I don't have experience with ruby to provide you with some code. Best wishes :)

Aboelnour
I'm not looking for "abstract" guidelines on Unix processes but a way to work around with the ruby PTY module. Your answer is pretty useless.
devlearn
so next time please make your question more specific:"If (you have any ideas) on (((how))) to get the spawned process return code, I'd really appreciate"hope you find your answer.
Aboelnour
Ok, sorry if the question was not clear enough for you, this really was a Ruby question, not general Unix. Maybe I shouldn't have flagged it as io and Linux ...
devlearn
A: 

Ok, here are some possible solutions for this problem :

  • use ruby 1.9.2 PTY.check() method

  • wrap the command line in a script

Unfortunately I can't use the latest version of ruby as so I used the wrapper solution, that echoes $? to a file at the end of the wrapper script. The exit code is read when the spawned child exits.

Of course if something interrupts the execution of the wrapper script itself, then we'll never get the result file ...

But at least this workaround can be used for 1.8.7/1.9.1 versions of Ruby

devlearn