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:


position startpos moves e2e4
go movetime 5000


~$ 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?

  • 1
    Did you add a linebreak after "go move 5000"? Sep 1 '15 at 10:47

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.

  • 1
    Thank you! My program seems to have stripped any trailing newlines, so I just added a line to explicitly append one after each command. Sep 1 '15 at 13:51
  • 2
    Does it work now? Can you actually pipe several commands at once? Sep 1 '15 at 14:02
  • 1
    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. Sep 4 '15 at 3:05
  • @Emporophobe It doesn't seem to work for me. Could you please give more details as to how were you able to do it? Jun 27 '20 at 17:04

Solution: Use 'expect' script as shown below. Tested with stockfish_8_x64_modern.

BlindKungFuMaster: "I think the main problem is that the uci protocol is a dialog." I agree!


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
  • Can windows batch scripts do the same? to simulate a dialog?
    – dbdb
    Aug 7 at 1:54

look into python-chess package. It includes a protocol for interacting with engines.

  • 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).
    – dbdb
    Aug 7 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.