Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a system command as sudo from R?

Tags:

r

Sometimes this works, sometimes not. It seems to depend on whether the system needs to prompt for a password. The more general question would be: is there a way for the user to provide input to a shell command from within R?

system('sudo npm install gitbook -g')

Note that my specific case is trying to install a node.js module. However, I think you can replicate it using a more trivial command.

system('sudo mkdir testdir')

Please note that this will sometimes work depending on whether sudo requires you to re-enter a password. Thanks.

like image 602
jbryer Avatar asked Apr 20 '14 19:04

jbryer


People also ask

How do I run a command in R console?

Type the relevant R function calls into the text file. To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.

How do I run sudo as root?

To use a "root" terminal, type "sudo -i" at the command line. The entire group of default graphical configuration tools in Kubuntu already uses sudo, so you will be prompted for your password if needed using kdesu, which is a graphical frontend to sudo.

What is sudo command in putty?

The sudo command allows you to run programs with the security privileges of another user (by default, as the superuser). It prompts you for your personal password and confirms your request to execute a command by checking a file, called sudoers , which the system administrator configures.


3 Answers

I can suggest two different solutions:

  1. Use gksudo, which will prompt the user for a password in a graphical interface. Here is how it works in practice:

    system('gksudo ls')

    • PRO:

      • It is secure, you don't have to handle the password yourself.
      • ....
    • CONS:

      • it won't work without a graphical interface.
      • gksudo was installed by default with linux brands I have tried, but YMMV: maybe some users won't have it.
      • ....
  2. Ask for the user password in R, and supply it with the proper sudo options: -k to always ask for the password, and -S to accept the password from the standard input. Here is how it works in practice:

    system('sudo -kS ls',input=readline("Enter your password: "))

    • PRO:

      • It does not rely on any other program.
      • ....
    • CONS:

      • I don't like the idea that a password gets manipulated by R: it looks like a bad idea.
      • ....

Other than that, I am not aware on any way to interactively communicate with a program started from R.

like image 50
Jealie Avatar answered Sep 28 '22 06:09

Jealie


Just to build up on @Jealie's response. I believe 1. Won't work in new versions of ubuntu.

But we can let Rstudio handle the password:

system("sudo -kS ls", input = rstudioapi::askForPassword("sudo password"))
like image 38
Gorka Avatar answered Sep 28 '22 06:09

Gorka


Since the gksudo utility mentioned in @jealie's answer is not maintained anymore (and thus missing from Ubuntu 18.04 onwards), one has to rely on pkexec instead as described here:

system("pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ls")

The same command using R's newer system2() function:

system2(command = "pkexec",
        args = "env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ls",
        stdout = TRUE,
        stderr = TRUE)

To run multiple commands in sequence with only a single password prompt, combine this with sudo and bash -c '...':

system2(command = "pkexec",
        args = paste0("env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY bash -c ",
                      "'sudo mkdir somedir; sudo ls -1l; sudo rm -R somedir'"),
        stdout = TRUE,
        stderr = TRUE)
like image 40
Salim B Avatar answered Sep 28 '22 06:09

Salim B