When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn’t work, and I need to set chmod to 775 or else I don’t have permission to read logs.
Why doesn’t this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();
Since you say
chmod 775for log file it works, it’s obvious your process doesn’t have permission.You can run your java with sudo:
Or just add
sudoas the first string in the array that you pass to bash process:Assuming user
oracleis in sudoers list and won’t ask for password, this will run just like how it runs in commandline when you usesudo.