4

I set up RStudio Server on an Amazon EC2 instance, which is charged by hour. I have to run long jobs overnight on this instance regularly. I would like to stop EC2 server when my R job is done to avoid charges for the hours I don't use.

How should I go about this?

Phil
  • 7,287
  • 3
  • 36
  • 66
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
  • 1
    What you want is to use a micro instance to host your R studio environment. This will have minimal cost (I believe first year is free). You can than use a package like Segue to dispatch any complicated computations off to Amazon EMR. Taking this approach your usage will only be billed while your job is running. – Dave Dec 10 '12 at 03:20
  • @davewolfs: thanks a lot for pointing me to this direction. I didn't even know it existed. Please post your comment as an answer b/c I believe it can be useful to a lot of R users. If you have time, I would love to hear more about this solution. – AdamNYC Dec 10 '12 at 03:53

2 Answers2

3

You might add your (Unix) username (which you use to log into the RStudio Server) to the /etc/sudoers file with NOPASSWD to allow such unsupervised action, for example:

username ALL = NOPASSWD: /sbin/shutdown

Then simply halt the computer with a system call, for example:

system('sudo shutdown -h now', wait = FALSE)
daroczig
  • 28,004
  • 7
  • 90
  • 124
  • @daroczig: I tried this but it returns `sudo: no tty present and no askpass program specified`. How do I a) find the user running RStudio Server, and b) add it to the `sudoers` file? – histelheim Jan 30 '15 at 14:33
0

I want to add to the above answer. You have to first tell the linux operating system on the instance to allow such a command without a password.

This can be done using the advice found in this other stack exchange quesiton.

If you are using Louis Aslett’s RStudio AMI, from the "terminal" tab in the Rstudio window, run the following code:

sudo visudo

This will open a file that can be editted to change the permission structure. At the very bottom of the file (scroll down with arrow keys) add the following line:

"rstudio ALL = NOPASSWD: /sbin/shutdown"

Then, to exit press "ctrl + x" and it will prompt you whether you want to save. Press "Y". It will then ask about some options and simply press "Enter"

This should then allow you to run the original solution from an R script or the R console:

system('sudo shutdown -h now', wait = FALSE)

Which will shutdown the system and cause the instance to enter the "Stopped" state on the AWS console.

M. Warden
  • 46
  • 4