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.
strace
on your program to see how it's messing with its stdin? That may give you clues on what's going on.echo -n
shouldn't provide any input anyway.