I am running R on EC2 spot instances and I need R to terminate the instance and cancel the spot request once the script has run.
我在EC2现场实例上运行R,我需要R来终止实例并在脚本运行后取消现场请求。
For that I have set the "Request ID" into an environmental variable in /.bashrc
and my plan was to simply call the following code into R once the script is ready
为此,我已将“请求ID”设置为/.bashrc中的环境变量,我的计划是在脚本准备好后简单地将以下代码调用到R中
system("ec2-cancel-spot-instance-requests $SIR")
The issue I am having is that R is not "seeing" the same environmental variables I seen when I type env
from outside R thus the command is not working.
我遇到的问题是R没有“看到”我从外部R键入env时看到的相同环境变量,因此命令无效。
I have checked and if I set my environmental variables at /etc/environment
R is able to see those variables, but here is the other problem. As those variables are dynamic (the instance ID and the request ID is different each time a spot instance is created), I am running a script to create them in the form of:
我已经检查过,如果我在/ etc / environment中设置我的环境变量,R就能看到这些变量,但这是另一个问题。由于这些变量是动态的(每次创建专色实例时实例ID和请求ID都不同),我运行的脚本以下列形式创建它们:
export SIR=`cat /etc/ec2_instance_spot_id.txt`
Where that file contains the dynamic ID
该文件包含动态ID的位置
So, how can I insert "dynamic" environmental variables into /etc/environment
? Or, how can I make R read the environmental variables at /.bashrc
?
那么,如何在/ etc / environment中插入“动态”环境变量?或者,如何让R读取/.bashrc中的环境变量?
Any tip in the right direction will be much appreciated!
任何正确方向的提示将非常感谢!
2 个解决方案
#1
10
You want Sys.getenv()
as in Sys.getenv("PATH")
, say.
你想要Sys.getenv()和Sys.getenv(“PATH”)一样。
Or for your example, try
或者为你的例子,试试吧
SIR <- Sys.getenv("SIR")
system(paste("ec2-cancel-spot-instance-requests", SIR))
As for setting variables at startup, see help(Startup)
to learn about ~/.Renvironment
etc
至于在启动时设置变量,请参阅帮助(启动)以了解〜/ .Renvironment等
#2
6
Using Sys.getenv()
you see all variables listed in the current environment.
使用Sys.getenv()可以看到当前环境中列出的所有变量。
However, they are different from those used in your current shell, for example specified in .profile.
但是,它们与当前shell中使用的不同,例如.profile中指定的那些。
To set the variables for R create a .Renviron
file in your home directory and write there
要为R设置变量,请在主目录中创建.Renviron文件并写入
MYDIRECTORY="/home/wherever"
After restarting R you will be able to access this variable with
重新启动R后,您将能够访问此变量
Sys.getenv("MYDIRECTORY")
#1
10
You want Sys.getenv()
as in Sys.getenv("PATH")
, say.
你想要Sys.getenv()和Sys.getenv(“PATH”)一样。
Or for your example, try
或者为你的例子,试试吧
SIR <- Sys.getenv("SIR")
system(paste("ec2-cancel-spot-instance-requests", SIR))
As for setting variables at startup, see help(Startup)
to learn about ~/.Renvironment
etc
至于在启动时设置变量,请参阅帮助(启动)以了解〜/ .Renvironment等
#2
6
Using Sys.getenv()
you see all variables listed in the current environment.
使用Sys.getenv()可以看到当前环境中列出的所有变量。
However, they are different from those used in your current shell, for example specified in .profile.
但是,它们与当前shell中使用的不同,例如.profile中指定的那些。
To set the variables for R create a .Renviron
file in your home directory and write there
要为R设置变量,请在主目录中创建.Renviron文件并写入
MYDIRECTORY="/home/wherever"
After restarting R you will be able to access this variable with
重新启动R后,您将能够访问此变量
Sys.getenv("MYDIRECTORY")