2

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?

6
  • Did you grant permissions to use sudo to the user executing the script? Commented Aug 14, 2013 at 9:36
  • yes I do. It is working fine when I execute it in terminal as a command. Commented Aug 14, 2013 at 9:37
  • But does it ask you for the password? You can use the NOPASSWD: expression in sudoers to avoid it. Commented Aug 14, 2013 at 9:44
  • 1
    You are only dumping the stdout to the screen. Dump also stderr and the exit status of the process. sudo may be giving an error message that you are not seeing. Commented Aug 14, 2013 at 9:46
  • @Joni I've included everything in my output here. Commented Aug 14, 2013 at 12:13

3 Answers 3

1

Nowadays most distros have sudo configured to require tty to execute.

try (within /etc/sudoers):

Defaults    requiretty

or

Defaults!/path/to/program !requiretty

You need to make sure sudo does not require a password for that user for that particular command (e.g. ls /) or you need to supply it to sudo.

UPDATE: btw, it is a good idea to read stderr as well so we don't have to guess what is wrong with command execution

Sign up to request clarification or add additional context in comments.

2 Comments

this command doesn't work for me. I get command not found error
@user1649068, edited answer to specify this is sudoers directive.
0

you should be able to pipe in a password to the sudo command.

1 Comment

password is not what I'm worried about. mine doesn't ask for one.
0

The command sudo -u root -S ls / prompts for the user password.

Something like echo '[password]' | sudo -u root -S ls / might do the trick.

Note: This requires hard-coding the password. Which is probably not the best idea.

EDIT: According to man sudo, the -u root option is unnecessary. Running sudo -S ls / will have the same effect.

2 Comments

java will execute it as a plain echo command and display output as [password] | sudo -u root -S ls /
@user1649068 I've edited the command. Like that it works on my machine (at the command prompt).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.