Discussion:
libssh2_channel_read() blocking in case of a background process
anirudh nair
2011-12-08 08:37:32 UTC
Permalink
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.

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

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

Hope my problem statement is clear.

How can I go about achieving this requirement?

Thanks
Anirudh
Dan Fandrich
2011-12-08 20:52:08 UTC
Permalink
Post by anirudh nair
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.
Post by anirudh nair
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.
Post by anirudh nair
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.
Post by anirudh nair
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.
Post by anirudh nair
Dan
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
anirudh nair
2011-12-09 08:41:31 UTC
Permalink
Hi Dan,

Thanks a lot for the reply.
Post by anirudh nair
I need to execute a remote script as below
Post by anirudh nair
//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
Post by anirudh nair
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.
Post by anirudh nair
Post by anirudh nair
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.
Post by anirudh nair
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
Post by anirudh nair
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.
Post by anirudh nair
Post by anirudh nair
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.
Post by anirudh nair
Dan
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Henrik Nordström
2011-12-09 09:45:33 UTC
Permalink
Post by anirudh nair
My only question is why is does libssh2_channel_read() wait even
though the script has returned?
Did you see my response earlier?

The server only closes the channel when it is no longer in use. Your
background started application keeps the channel open as it's attached
to stdin/stdout/stderr of the application.

You need to adjust how you start the background application to make sure
to detach it from stdin/stdout/stderr.

some_program </dev/null >/dev/null 2>&1 &


Regards
Henrik

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Loading...