我正在使用宝石。我想在计划任务之前设置我的env变量。下面代码中的第二行是我想在schedule.rb文件中运行的命令。但看起来我无法完成它所以我评论了它并以不同的方式尝试。似乎仍然没有任何效果。在ruby代码中打印env似乎正确地显示了所需的变量,但在运行'bundle exec when'并使用env命令检查终端后,变量似乎不存在。这有什么不对?
every :hour do
# Run shell script to assign variables and continue the rake task
#system "for line in `cat config/myEnvFile.env` ; do export $line ; done"
f = File.open("config/myEnvFile.env", "r")
f.each_line do |line|
j = line.split(" ")
arr = j.first.split("=")
system "export #{arr[0]}=#{arr[1]}"
system "env"
# This line below is also another way but still nothing works
ENV[arr[0]] = arr[1]
end
rake "task:continue_doing_my_task"
end
答案 0 :(得分:0)
every
- 块中的代码在whenever --update-crontab
运行时执行,而不是每小时执行一次。
您需要使rake "task:continue_doing_my_task"
生成的命令加载这些变量,使用bash语法(变得丑陋)在线导出它们,或者使用@Anthony注释中建议的dotenv。
查看我对follow-up question的回答,其中详细解释了这一点。