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?
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)