What is the correct command format for the Java ProcessBuilder sudo bash command?

How to execute such a command (using sudo) from Java ProcessBuilder? (I do not need to include a password, because a regular user has permission to run myscript.log as root without a password.)

sudo bash /home/me/path/myscript.sh arg1 arg2 arg3 >> /var/log/path/myscript.log 2>&1

My question is, how are the elements of this command passed to the Java constructor ProcessBuilder?

For example, "sudo" is the first argument to ProcessBuilderctor, last or somewhere else? And how do you know where they are going, and what elements of the team become arguments for ProcessBuilder?

+3
source share
1 answer

The problem is that the redirect is not performed on the bash command that you are invoking.

usin -c bash:

sudo bash -c "/home/me/path/myscript.sh arg1 arg2 arg3 >> /var/log/path/myscript.log 2>&1"

java ProcessBuilder , :

new ProcessBuilder("sudo", "bash", "-c", "/home/me/path/myscript.sh arg1 arg2 arg3 >> /var/log/path/myscript.log 2>&1");

ProcessBuilder sudo 3 . sudo : bash -c "...".

, sudo.

+3

All Articles