Answers.Body: <p>Edit: I think the main problem is that the uci protocol is a dialog. You have to wait for the "readyok" output from the engine before you can issue another command. Probably you just cannot forcefeed all commands in one go.</p>
  
  <p>To quote the uci protocol:</p>
  
  <p>"* all command strings the engine receives will end with '\n', …"</p>
  
  <p>Possibly you just forgot about that in your second line. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-01T13:51:48.317Z
Answers.Text: Thank you! My program seems to have stripped any trailing newlines, so I just added a line to explicitly append one after each command.
Answers.UserDisplayName: Emporophobe
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-01T14:02:40.693Z
Answers.Text: Does it work now? Can you actually pipe several commands at once?
Answers.UserDisplayName: BlindKungFuMaster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-04T03:05:39.720Z
Answers.Text: It does seem to work, I now write the two commands and newlines to the text file and pipe it all at once; I haven't tested it thoroughly, but it at least works enough to respond to moves and play a full game. Thanks again for your answer.
Answers.UserDisplayName: Emporophobe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-27T17:04:28.467Z
Answers.Text: @Emporophobe It doesn't seem to work for me. Could you please give more details as to how were you able to do it?
Answers.UserDisplayName: Ritesh Singh
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-01T10:49:50.710Z
Answers.LastActivityDate: 2015-09-01T13:47:55.463Z
Answers.LastEditDate: 2015-09-01T13:47:55.463Z
Answers.UserDisplayName: BlindKungFuMaster
Answers.Body: <p><strong>Solution</strong>: Use 'expect' script as shown below. Tested with stockfish_8_x64_modern. </p>
  
  <blockquote>
    <p>BlindKungFuMaster: "I think the main problem is that the uci protocol is a dialog."
      <strong>I agree!</strong></p>
  </blockquote>
  
  <pre><code>#!/usr/bin/expect
  
  spawn ~/stockfish_8_x64_modern
  
  expect -timeout 1000  Linscott 
  
  send "setoption name Ponder  value false \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "setoption name Hash    value 2048 \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "setoption name Threads value 8    \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "setoption name MultiPV value 4    \r isready \r"
  
  expect -timeout 1000  readyok
  
  set row8 "rnbqkbnr"
  
  set row7 "pppppppp"
  
  set row6 "11111111"
  
  set row5 "11111111"
  
  set row4 "11111111"
  
  set row3 "11111111"
  
  set row2 "PPPPPPPP"
  
  set row1 "RNBQKBNR"
  
  
  send "position fen $row8/$row7/$row6/$row5/$row4/$row3/$row2/$row1 w KQkq - 0 1 moves e2e4 \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "go infinite \r" 
  
  expect -timeout 999999  readyok
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-07T01:54:26.877Z
Answers.Text: Can windows batch scripts do the same? to simulate a dialog?
Answers.UserDisplayName: dbdb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-21T03:57:57.863Z
Answers.LastActivityDate: 2017-03-22T03:19:09.880Z
Answers.LastEditDate: 2017-03-22T03:19:09.880Z
Answers.UserDisplayName: Alexey Lvov
Answers.Body: <p>look into python-chess package. It includes a protocol for interacting with engines.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-07T01:55:48.100Z
Answers.Text: Do you know if it is strict UCI or also the other commands an engine can still take (SF has four of them, mostly for debugging).
Answers.UserDisplayName: dbdb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-21T12:59:49.563Z
Answers.LastActivityDate: 2017-03-21T12:59:49.563Z
Answers.UserDisplayName: nak3c
Answers.Body: <p>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 <code>cat</code> 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 <code>a7a6</code> in your example).</p>
  <p>The simple solution is to delay the input stream with a <code>sleep</code> until Stockfish is done with it's analysis. That can be done by using two commands to generate Stockfish's input stream. Example:</p>
  <pre><code>( cat sf.txt; sleep 5001; ) | stockfish
  </code></pre>
  <p>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).</p>
  <p>However, when you are limiting the search by nodes or by depth (such as in <code>go depth 30</code>), 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 <code>bestmove</code> string to show up on that file:</p>
  <pre><code>(
      echo &quot;setoption name Hash value 1024&quot;;
      echo &quot;setoption name threads value 10&quot;;
      echo &quot;position fen 'rn3rk1/pbppq1pp/1p2pb2/4N2Q/3PN3/3B4/PPP2PPP/R3K2R w KQ - 6 11'&quot;;
      echo &quot;go depth 30&quot;;
      (grep -m 1 &quot;bestmove&quot; &lt;(tail -f analysis.txt))&gt; /dev/null
  ) | stockfish | tee /dev/stderr &gt; analysis.txt
  </code></pre>
  <p>Yeah, it is literally catching it's own tail, but it does get to an end. :-)</p>
  <p>Explanation:</p>
  <ul>
  <li><code>tail -f</code> watches the Stockfish's output file</li>
  <li><code>grep -m 1 &quot;bestmove&quot;</code> filters the watched file until that string is found, then quits</li>
  <li><code>&gt; /dev/null</code> is to prevent grep's output to be resent to Stockfish</li>
  <li><code>| tee /dev/stderr</code> is optional, for you to see Stockfish's output in real time</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-11T05:31:24.137Z
Answers.LastActivityDate: 2022-06-11T05:31:24.137Z
Answers.UserDisplayName: Murilo Perrone
Body: <p>I'm trying to write a Linux command line UCI interface for Stockfish. My program writes a bunch of commands to a text file and then tries to pipe them to Stockfish via the command line. My problem is, whenever I try to analyse a position, the program exits almost immediately:</p>
  
  <p>sf.txt:</p>
  
  <pre><code>position startpos moves e2e4
  go movetime 5000
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>~$ stockfish &lt; sf.txt
  Stockfish 261014 64 by Tord Romstad, Marco Costalba and Joona Kiiski
  info nodes 0 time 2
  bestmove a7a6 ponder (none)
  </code></pre>
  
  <p>As you can see, the program exits in 2 ms instead of waiting for the analysis to finish. Has anyone delt with this issue before? Is there a preferred way of interfacing with Stockfish?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-01T10:47:50.193Z
Comments.Text: Did you add a linebreak after "go move 5000"?
Comments.UserDisplayName: BlindKungFuMaster
Tags: <stockfish><programming><uci>
Title: Piping Stockfish input
ThreadText: # Piping Stockfish input
  
  (Asked by: username_0 on 2015-09-01)
  
  I'm trying to write a Linux command line UCI interface for Stockfish. My program writes a bunch of commands to a text file and then tries to pipe them to Stockfish via the command line. My problem is, whenever I try to analyse a position, the program exits almost immediately:
  
  sf.txt:
  
  ```
  position startpos moves e2e4
  go movetime 5000
  
  ```
  
  output:
  
  ```
  ~$ stockfish < sf.txt
  Stockfish 261014 64 by Tord Romstad, Marco Costalba and Joona Kiiski
  info nodes 0 time 2
  bestmove a7a6 ponder (none)
  
  ```
  
  As you can see, the program exits in 2 ms instead of waiting for the analysis to finish. Has anyone delt with this issue before? Is there a preferred way of interfacing with Stockfish?
  
  **Tags:** stockfish, programming, uci
  
  > **Comments:**
  > **username_1**: Did you add a linebreak after "go move 5000"?  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2015-09-01. Score: 3 | Accepted answer
  
  Edit: I think the main problem is that the uci protocol is a dialog. You have to wait for the "readyok" output from the engine before you can issue another command. Probably you just cannot forcefeed all commands in one go.
  
  To quote the uci protocol:
  
  "\* all command strings the engine receives will end with '\n', …"
  
  Possibly you just forgot about that in your second line.
  
  > **Comments:**
  > **username_0**: Thank you! My program seems to have stripped any trailing newlines, so I just added a line to explicitly append one after each command.  
  > **username_1**: Does it work now? Can you actually pipe several commands at once?  
  > **username_0**: It does seem to work, I now write the two commands and newlines to the text file and pipe it all at once; I haven't tested it thoroughly, but it at least works enough to respond to moves and play a full game. Thanks again for your answer.  
  > **username_2**: @username_0 It doesn't seem to work for me. Could you please give more details as to how were you able to do it?  
  
  ---
  
  ### Answer by username_3 on 2017-03-21. Score: 6
  
  **Solution**: Use 'expect' script as shown below. Tested with stockfish\_8\_x64\_modern.
  
  > username_1: "I think the main problem is that the uci protocol is a dialog."
  > **I agree!**
  
  ```
  #!/usr/bin/expect
  
  spawn ~/stockfish_8_x64_modern
  
  expect -timeout 1000  Linscott 
  
  send "setoption name Ponder  value false \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "setoption name Hash    value 2048 \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "setoption name Threads value 8    \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "setoption name MultiPV value 4    \r isready \r"
  
  expect -timeout 1000  readyok
  
  set row8 "rnbqkbnr"
  
  set row7 "pppppppp"
  
  set row6 "11111111"
  
  set row5 "11111111"
  
  set row4 "11111111"
  
  set row3 "11111111"
  
  set row2 "PPPPPPPP"
  
  set row1 "RNBQKBNR"
  
  send "position fen $row8/$row7/$row6/$row5/$row4/$row3/$row2/$row1 w KQkq - 0 1 moves e2e4 \r isready \r"
  
  expect -timeout 1000  readyok
  
  send "go infinite \r" 
  
  expect -timeout 999999  readyok
  
  ```
  
  > **Comments:**
  > **username_4**: Can windows batch scripts do the same? to simulate a dialog?  
  
  ---
  
  ### Answer by username_5 on 2017-03-21. Score: 2
  
  look into python-chess package. It includes a protocol for interacting with engines.
  
  > **Comments:**
  > **username_4**: Do you know if it is strict UCI or also the other commands an engine can still take (SF has four of them, mostly for debugging).  
  
  ---
  
  ### Answer by username_6 on 2022-06-11. Score: 1
  
  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
  
  
  ---
  
  