I have this code to execute a command with and without using sudo
option
String sudoScript = "sudo -u root -S ls /";
String script = "ls /";
try {
System.out.println("===================================");
System.out.println("command="+sudoScript);
Process p1 = Runtime.getRuntime().exec(sudoScript);
System.out.println("p2.waitFor()="+p1.waitFor());
BufferedReader stdInput1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
while ((sudoScript = stdInput1.readLine()) != null) {
System.out.println(script);
}
System.out.println("===================================");
System.out.println("command="+script);
Process p2 = Runtime.getRuntime().exec(script);
System.out.println("p2.waitFor()="+p2.waitFor());
BufferedReader stdInput2 = new BufferedReader(new InputStreamReader(p2.getInputStream()));
while ((script = stdInput2.readLine()) != null) {
System.out.println(script);
}
} catch (Exception e) {
e.printStackTrace();
}
And this is the kind of output I get-
===================================
command=sudo -u root -S ls /
p2.waitFor()=1
===================================
command=ls /
p2.waitFor()=0
bin
boot
cgroup
data
dev
etc
home
home.save
lib
lost+found
media
mnt
null
opt
opt.save
proc
root
sbin
selinux
srv
sys
tmp
usr
var
If you observe, I'm not able to get the same output using sudo
command. Is there something I've missed here?