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

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

From: anirudh nair <anirudh.anu01_at_gmail.com>
Date: Fri, 9 Dec 2011 14:11:31 +0530

Hi Dan,

Thanks a lot for the reply.

> 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.
>

 My only question is why is does libssh2_channel_read() wait even though
the script has returned?
 The primary goal of the script is to start an application(service) and to
return the status of the startup.
 The script checks if the application is up by greping through the output
of ps and finally return the status.

>
> > 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.
>

As I said, I'm expecting the return code of the script and not that of the
process.

> > 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
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2011-12-09