Mongosh --eval freezes the shell

I frequently used to run ‘mongo’ with --eval parameter, mostly in some automation scripts, for example simple ‘mongo --eval 1’ to see if mongod is already fully started.
When I try the same with mongosh the execution of command never ends. It prints some welcome information, then the literal ‘1’ and freezes. I can’t stop it by either ctrl+c, ctrl+d or ctrl+z. Only killing it from another shell helps.
IT is the same with any other command.
Is this something that you know about? Maybe I’m doing something wrong?


@Andrzej_Podgorski How are you killing the process (i.e. with which signal)? Can you see if there’s a spike in CPU usage? Which version of mongosh are you using? Can you look at the log file (~/.mongodb/mongosh/_log) and see if there’s any relevant information in there?

1 Like

I’m able to kill it even with SIGTERM. I don’t see any spike in CPU usage.
I see a problem on both mongosh 1.0.4 and 1.0.5 on Ubuntu 18.04 on a few different machines. Usage of --nodb or --norc does not change the behavior.
I don’t see anything relevant in the log. The last entry is
{“level”:30,“time”:1630050655432,“pid”:15175,“hostname”:"",“name”:“mongosh”,“msg”:“mongosh:evaluate-input {“input”:“1”}”,“v”:1}

And I have the same on Windows using powershell

any ideas here folks?

@Andrzej_Podgorski The problem is that this is hard to debug without a reproduction – I’m also running Ubuntu but haven’t had an issue like this so far.

I guess you could explicitly use something like mongosh --eval "print(1); exit();" or mongosh --eval "print(1); process.exit();" to explicitly stop the shell? I know it’s not a great solution, but I’m also not sure how else to move forward here.

If you are willing to debug this in depth (which we are grateful for but obviously don’t expect of you) on Ubuntu, running mongosh under strace might be pretty helpful (although the log it generates would presumably be huge).