Subject: Re: libssh2_channel_read() blocking in case of a background process

Re: libssh2_channel_read() blocking in case of a background process

From: Dan Fandrich <>
Date: Thu, 8 Dec 2011 12:52:08 -0800

On Thu, Dec 08, 2011 at 02:07:32PM +0530, anirudh nair wrote:
> Hi all,
> I need to execute a remote script as below
> //script begins- only psuedo_code
> some_process &(as a background process)
> if (some_process is up)
> echo "Application is running"
> exit 1
> else
> echo "Application not started"
> exit 2
> endif
> //script ends
> But I see that  libssh2_channel_read() blocks while waiting for some_process to
> return.

You're requesting data to be read, so, naturally, it can't return until it
comes in.

> But If I make it non blocking i.e
> libssh2_session_set_blocking(session, 0);

If it's made nonblocking, then libssh2_channel_read() is just going to
return LIBSSH2_ERROR_EAGAIN until data does come in. If your program doesn't
explicitly poll to wait for a response from the server, then there's no way
to know what that remote script is doing.

> the below code always returns 0
> nReturnCode = libssh2_channel_get_exit_status( channel );
> Ideally I want the read not to block and at the same time return the correct
> return code i.e 1

I don't understand what you want here. It sounds like you magically want to
know what the return code of the process is going to be without waiting for
that process to run and the response to be returned.

> Hope my problem statement is clear.

Not unless it involves breaking the laws of physics. Are you just trying to
get some work done in the program while the remote script is running? i.e.
rather than blocking while waiting for the response, you want to do something
else while waiting? If that's what you want, then you should set the session
to nonblocking then keep calling libssh2_channel_read() until it stops
returning LIBSSH2_ERROR_EAGAIN, interleaving those calls with whatever else
you're trying to do.

>>> Dan
Received on 2011-12-08