zeroturnaround / zt-exec

ZeroTurnaround Process Executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

An error InvalidExitValueException while executing the unix command

arliangtw opened this issue · comments

When I execute the ubuntu command through suno, an exception will throws.
String output = new ProcessExecutor().command("sudo","touch","/opt/cc") .redirectInput(new ReaderInputStream(new StringReader("mypassword\n"))) .timeout(30, TimeUnit.SECONDS) .readOutput(true) .exitValues(0) .execute() .getOutput() .getUTF8();

The exception is as follows
org.zeroturnaround.exec.InvalidExitValueException: Unexpected exit value: 1, allowed exit values: [0], executed command [sudo, touch, /opt/cc], output was 54 bytes: sudo: no tty present and no askpass program specified

I know that you can modify sudoers.d so that users do not have to enter a password.
But I do not want to do this, change the system's security policy.
Thank you!

Try command("sudo","--stdin","touch","/opt/cc")

   -S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.  The password must
               be followed by a newline character.

This work, Thank you for your help.