简体   繁体   English

如何使用 rbenv 和 cron 运行 Ruby 脚本

[英]How to run a Ruby script using rbenv with cron

I'm trying to run a Ruby script using rbenv with cron.我正在尝试使用 rbenv 和 cron 运行 Ruby 脚本。 I know that I need to load rbenv in order to have the right Ruby version loaded.我知道我需要加载 rbenv 才能加载正确的 Ruby 版本。

I've tried options like this:我试过这样的选项:

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

but as the session is not interactive, I'm not having the right Ruby version.但由于会话不是交互式的,我没有正确的 Ruby 版本。 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'

It didn't work neither.它也不起作用。 Then I wrote a loader, which only load rbenv in the current shell but it doesn't work.然后我写了一个加载器,它只在当前shell中加载rbenv但它不起作用。

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

Now I'm searching for another way to load it ... any ideas?现在我正在寻找另一种加载方式......有什么想法吗?

I've found a solution to load rbenv.我找到了加载 rbenv 的解决方案。 Either with a loader importing rbenv to the PATH :使用加载器将 rbenv 导入到 PATH :

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

The '.'这 '。' before '$HOME/.rbenv/loader.sh' is important, it runs the script in the current shell在 '$HOME/.rbenv/loader.sh' 很重要之前,它会在当前 shell 中运行脚本

Or without loader, which is better :或者没有装载机,哪个更好:

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

A better solution is to simply use the command bash -lc command.更好的解决方案是简单地使用命令bash -lc命令。 This will read your bash profile file, which would setup rbenv.这将读取您的 bash 配置文件,该文件将设置 rbenv。 From the bash man page:bash手册页:

-l Make bash act as if it had been invoked as a login shell -l 使 bash 就像作为登录 shell 被调用一样

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists.当 bash 作为交互式登录 shell 或作为带有 --login 选项的非交互式 shell 调用时,它首先从文件 /etc/profile 读取并执行命令(如果该文件存在)。 After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的命令中读取和执行命令。 The --noprofile option may be used when the shell is started to inhibit this behavior.当 shell 启动时可以使用 --noprofile 选项来禁止这种行为。

Even though kmmndr's answer is correct, I also like the bash -l -approach.尽管 kmmndr 的答案是正确的,但我也喜欢bash -l -approach。

Opening a non-interactive login shell keeps things simpler and since my Rails applications and Ruby scripts all run under the same user, overhead is not a problem.打开非交互式登录 shell 让事情变得更简单,因为我的 Rails 应用程序和 Ruby 脚本都在同一用户下运行,所以开销不是问题。

So instead of所以代替

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

I do我愿意

*/1 * * * * /bin/bash -lc 'cd /data/app/; ruby -v'

As noted in the above answer, bash -l will act as if you login normally, which means that your rbenv environment will already be set up (as long as you have the appropriate lines in your .bashrc , .bash_profile of /etc/profile.d/* ).正如上面的回答所指出的, bash -l行为就像你正常登录一样,这意味着你的 rbenv 环境已经设置好了(只要你的.bashrc.bash_profile of /etc/profile.d/*有适当的行/etc/profile.d/* )。

If you need more detail, I wrote a blog post about this topic.如果您需要更多详细信息,我写了一篇关于此主题的博客文章

For my backup script in Ruby I simply use ~/.rbenv/bin/rbenv exec ruby [options] /path/to/ruby/script.rb .对于我在 Ruby 中的备份脚本,我只需使用~/.rbenv/bin/rbenv exec ruby [options] /path/to/ruby/script.rb Try this:尝试这个:

* * * * * ~/.rbenv/bin/rbenv exec ruby -v > ~/rbenv-ruby-version.txt

@Kelvin 的这个答案对我有用:

*/1 * * * * PATH=$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH ruby -v >> ~/test.out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM