The problem there is that stockfish process quits as soon as the input stream is closed. And that happens instantaneously, because the input stream is equivalent to a cat
command. As a result, Stockfish never gets a chance to run the search tree. So Stockfish just outputs the very first move in it's list (which is a7a6
in your example).
The simple solution is to delay the input stream with a sleep
until Stockfish is done with it's analysis. That can be done by using two commands to generate Stockfish's input stream. Example:
( cat sf.txt; sleep 5001; ) | stockfish
That works nice if you know the exact amount of time Stockfish will take, which is 5000 seconds in your example (notice I added 1 second just to be on the safe side).
However, when you are limiting the search by nodes or by depth (such as in go depth 30
), you don't know how much time Stockfish will take. In such case, the trick above won't be optimal, as it will either interrupt too early or keep sleeping for too long. So, instead you can use a more elaborated solution, which includes sending Stockfish's output to a file and making the input process wait for the bestmove
string to show up on that file:
(
echo "setoption name Hash value 1024";
echo "setoption name threads value 10";
echo "position fen 'rn3rk1/pbppq1pp/1p2pb2/4N2Q/3PN3/3B4/PPP2PPP/R3K2R w KQ - 6 11'";
echo "go depth 30";
(grep -m 1 "bestmove" <(tail -f analysis.txt))> /dev/null
) | stockfish | tee /dev/stderr > analysis.txt
Yeah, it is literally catching it's own tail, but it does get to an end. :-)
Explanation:
tail -f
watches the Stockfish's output file
grep -m 1 "bestmove"
filters the watched file until that string is found, then quits
> /dev/null
is to prevent grep's output to be resent to Stockfish
| tee /dev/stderr
is optional, for you to see Stockfish's output in real time