在rails_root之外运行rake/Incron不为rails运行rake或bundle命令

2 人关注

我在/home/myuser/watchDir/myapp里有一个rails应用程序,一个incron作业被设置为监视./watchDir的修改。 一旦被触发,incron将运行一个脚本,/usr/local/bin/myscript.sh。 这是我唯一能让incron运行脚本的地方。 在该脚本中,我调用了在我的根应用程序中运行 rake bundle 命令。 脚本正在运行(我有一个测试),但 bundle rake 命令都默默地失败了。 我对Linux相当陌生,互联网上的研究已经给出了一些解决方案。 我的脚本中都有绝对路径。 我试着把我的bash_profile添加到scripts/incron命令中。 我试着让incron脚本运行位于我主目录下的另一个脚本。 所有的脚本都是可执行的。 我试着用 --gemfile 选项来替换 bundle ,但这并不奏效。 有没有人知道我在这里要做什么? 基本上,我想在RAILS_ROOT之外运行 bundle rake 命令。 我还想知道incron是否使rails命令的使用变得复杂。 谢谢。

EDIT:

以下是相关文件。

Incrontab。

/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE /bin/bash /usr/local/bin/runT.sh  $@/$#

我还试过这个。

/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE source '/home/myuser/.bash_profile && /bin/sh /usr/local/bin/runT.sh'  $@/$#

这是它所调用的脚本。

#!/bin/bash
mkdir /home/myuser/worked  #This is to ensure that that incron is running and executing this script
cd /home/myuser/watchDir/myapp
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/bundle install --gemfile /home/myuser/watchDir/myApp/Gemfile
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/rake -f /home/myUser/watchDir/myApp

我的.bash_profile文件。

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
source ~/.profile
    
5 个评论
Beel
把你的incron脚本的内容贴出来可能会有帮助。 没有你的脚本,我猜测,但也许在你运行 rake bundle 命令的脚本的顶部添加一个 cd /path/to/approot
@Beel 是的,我试过了,我会把脚本贴出来的。
Beel
关于incron的棘手之处(似乎你已经发现了)是,脚本是在一个干净的环境中运行的。 所以你需要建立一个环境。 你的第二个例子试图通过获取用户tester的.bash_profile来做到这一点。 你必须这样做,但你必须为用户myuser这样做。 incron应该在某个地方有日志记录 -- 看看日志中是否有什么东西? 另外,由于你使用的是RVM,你的bash_profile应该是rvm设置脚本的来源。 对不起,我今天不在Mac上,所以不能测试确切的设置,但请注意。
@Beel 我修复了这个问题中的第二个incrontab条目......在我的实际incrontab中,都是同一个用户的。 我曾试图检查日志,但在/var/log中找不到,不过我肯定能找到,所以我会继续找。 我将对bash_profile进行修改,这似乎是我遗漏的一个重要细节。 谢谢你的帮助,我将公布任何成功/失败的情况。
Beel
另外,如果你把你的.bash_profile贴出来可能会有帮助。
ruby-on-rails
linux
bash
rake-task
incron
user3195786
user3195786
发布于 2014-03-14
1 个回答
Beel
Beel
发布于 2014-03-14
已采纳
0 人赞同

总结我最后的评论......把你的 icrontab 条目改为。

/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE /bin/bash /usr/local/bin/runT.sh  $@/$#

而剧本要。

#!/bin/bash
source /home/myuser/.bash_profile