[SOLVED] `exec` hangs when command is executed from Java/Groovy

Problem:

The following command never finishes when executed from Java: (example using in Groovy)

println "lxc exec example-test -- echo Hi".execute().text

What I have tested:

  • Java Runtime.getRuntime().exec() also halts when using process.waitFor().
  • Any other command like: lxc info example-test, etc. has not problem
  • "echo Hi".execute().text returns ‘Hi’ as expected.
  • Running the same command from bash returns immediately.
  • Interestingly, running it inside perl, the code won’t return unless I press enter.

Checking the Java native source code (java.io), it seems that BufferedInputStream.readBytes its waiting for a termination character that is never returned. However I can see in the buffer the value: “Hi\n” (followed by zeros) just before waiting forever.

Any suggestions on how to debug or make it work?

That’s probably because that environment provides some bogus stdin which causes lxc to hang during read from stdin.

Did you try using -n, -T or --mode non-interactive? Chances are that one of those will make this work.

Thanks for your reply, I really appreciate it.

Yes, -n did the trick. This is the command I applied:

lxc exec example-test -n -- echo 'Hi'

Thanks!