How can I work around the program failing if there is *any* stdin?

Rogach

For example, the following works fine:

/usr/bin/program

It produces some output, and gets to result.

But if I invoke it like this:

echo -n | /usr/bin/program

or this

echo -n | bash -c "/usr/bin/program"

or even this:

echo -n | bash -c "wc -c; /usr/bin/program"

It produces some lines of output, then fails. I have no access to source of the program, so I can't even look what could cause this behavior.

And when I try to invoke it from the python script, I get the same stuff:

echo | python -c 'from subprocess import call; call("/usr/bin/program", shell=True)'

(version without "echo" prepended works fine)

I don't even have the faintest idea why that could be happening. Stdin is going to be open even if I don't explicitly specify where the program should read from, so that shouldn't be the cause.

Is there any way to work around this issue?

EDIT:

The last four lines from strace output - the only ones that differ:

# without echo
select(1, [0], NULL, NULL, {0, 0})      = 0 (Timeout)
select(1, [0], NULL, NULL, {0, 0})      = 0 (Timeout)
select(1, [0], NULL, NULL, {0, 0})      = 0 (Timeout)
select(1, [0], NULL, NULL, {0, 0})      = 0 (Timeout)
...

# with echo
select(1, [0], NULL, NULL, {0, 0})      = 1 (in [0], left {0, 0})
write(4, "\0\0\0j\0\0\0\3\0\0\0\4\0\0\0\0\377\377\377\377", 20) = 20
write(3, "\0\0\0j\0\0\0\3\0\0\0\4\0\0\0\0\377\377\377\377", 20) = 20
exit_group(1)                           = ?

PARTIAL SOLUTION:

sleep 20 | /usr/bin/program

Seems that program waits for something to happen at stdin, and exits if it encounters a newline or EOF (we can see it from select call in strace output - it timeouts if input comes from "real" user). So we needed a program that doesn't write anything to stdin, while still keeping it open - sleep does the job.

Jander

Here's a Perl one-liner that will do what you want:

perl -e '$SIG{CHLD} = sub{exit 0}; open $fh, "|-", @ARGV or die; sleep 20 while 1;' /usr/bin/program

It's essentially the same as a mythical* sleep forever | /usr/bin/program, except it also watches for the program to finish, and will quit immediately when it does. If /usr/bin/program needs any args, you can tack them onto the end of the line.

*sleep forever doesn't work, but GNU sleep will sleep forever if you sleep inf!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I work around these permission problems?

How can I work around a RefCell issue?

How can I work around a KeyError in Python?

How can I work around this onmessage error?

How can I fix my program so that it can work with any number of rows and column user asks?

How can I test a program reading from stdin in GHCi?

How can I redirect the STDOUT to STDIN in a C program

how i can install any program with terminal

How can I make this program work properly?

How can i make this program work?

How can I invoke a program that needs data on stdin with sudo (w/ password also on stdin) from Python?

How can I implement a work-around for the let block?

How can I work around this apparent EhCache deadlock?

How can I work around Argo output parameter size limit?

How can I work around overflow error in matplotlib?

How can I work around Excel crashing with certain SELECT statements?

Why are UNIX shells like this and how can I work around it?

How can I make a library face to any other program language?

Why can I not access var i like this and how do I work around it?

how can I add collision to my object and around the screen in my program?

How can i stop the program from skipping my check the second time around?

How can I set up Eclipse to work with a Quickly program?

How can I make this big chunk of program smaller ? Will it work?

How can i properly read an input from stdin with a buffer without truncating any words?

How can I work around (or with) this function returning hexadecimal characters when I don't want it to? (Python 3)

How can i check if some of my work items have a test case failing

How can I rotate any shape or point on an HTML5 canvas around an arbitrary point?

How can I make my function work for any number?

How can I make a macro work on any container?

TOP Ranking

HotTag

Archive