如何使用 rbenv 和 cron 运行 Ruby 脚本 [英] How to run a Ruby script using rbenv with cron

查看:20
本文介绍了如何使用 rbenv 和 cron 运行 Ruby 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rbenv 和 cron 运行 Ruby 脚本.我知道我需要加载 rbenv 才能加载正确的 Ruby 版本.

I'm trying to run a Ruby script using rbenv with cron. I know that I need to load rbenv in order to have the right Ruby version loaded.

我试过这样的选项:

*/10 * * * */bin/bash -c 'source $HOME/.bashrc;cd/数据/应用程序;红宝石 -v' >>/tmp/logfile.txt 2>&1

*/10 * * * * /bin/bash -c 'source $HOME/.bashrc; cd /data/app; ruby -v' >> /tmp/logfile.txt 2>&1

但由于会话不是交互式的,我没有正确的 Ruby 版本.我找到了这样的例子:

but as the session is not interactive, I'm not having the right Ruby version. I've found example like this:

15 14 1 * * export BASH_ENV=/path/to/environment &&/full/path/to/bash -c '/full/path/to/rvm_script.rb'

15 14 1 * * export BASH_ENV=/path/to/environment && /full/path/to/bash -c '/full/path/to/rvm_script.rb'

也没有用.然后我写了一个loader,在当前shell中只加载rbenv但是不行.

It didn't work neither. Then I wrote a loader, which only load rbenv in the current shell but it doesn't work.

*/1 * * * */bin/bash -c '$HOME/.rbenv/loader.sh ;cd/数据/应用程序/;红宝石 -v ' >>/tmp/logfile.txt 2>&1

*/1 * * * * /bin/bash -c '$HOME/.rbenv/loader.sh ; cd /data/app/; ruby -v ' >> /tmp/logfile.txt 2>&1

现在我正在寻找另一种加载方式......有什么想法吗?

Now I'm searching for another way to load it ... any ideas?

推荐答案

我找到了加载 rbenv 的解决方案.使用加载器将 rbenv 导入到 PATH 中:

I've found a solution to load rbenv. Either with a loader importing rbenv to the PATH :

*/1 * * * */bin/bash -c '.$HOME/.rbenv/loader.sh ;cd/数据/应用程序/;红宝石 -v'

*/1 * * * * /bin/bash -c '. $HOME/.rbenv/loader.sh ; cd /data/app/; ruby -v'

'.'在 '$HOME/.rbenv/loader.sh' 很重要之前,它会在当前 shell 中运行脚本

The '.' before '$HOME/.rbenv/loader.sh' is important, it runs the script in the current shell

或者没有加载器,哪个更好:

Or without loader, which is better :

*/1 * * * */bin/bash -c 'export PATH="$HOME/.rbenv/bin:$PATH" ;eval "$(rbenv init -)";cd/数据/应用程序/;红宝石 -v'

*/1 * * * * /bin/bash -c 'export PATH="$HOME/.rbenv/bin:$PATH" ; eval "$(rbenv init -)"; cd /data/app/; ruby -v'

这篇关于如何使用 rbenv 和 cron 运行 Ruby 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆