开发者

Wrong ruby path used in rake task executed as cron through whenever

I am trying to use the whenever gem to execute a couple of rake tasks. It appears to set up the cron tasks correctly - info here is from the mail output that is produced. Here's an example of the command executed:

/bin/bash -l -c 'cd /path/to/deployed/app && RAILS_ENV=production

rake clean:my:task --silent'

And here are some of the environment variables:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>

And here is the error that follows:

/bin/bash: /usr/bin/rake: /usr/bin/ruby: bad interpreter: No such fil开发者_StackOverflow社区e or directory

When i'm logged in i can run these rake tasks from the same directory, but when i run

which ruby

i get

/usr/local/bin/ruby

There does appear to be a 'rake' in /usr/bin, but running

/usr/bin/rake -T

gives me the same error:

-bash: /usr/bin/rake: /usr/bin/ruby: bad interpreter: No such file or directory

What would be the best way to resolve this?


Is /usr/local/bin/ in your path? When you ran which ruby you got /usr/local/bin/ruby but when you run rake it's looking for /usr/bin/ruby

Or you could just symlink ruby like:

sudo ln -s /usr/local/bin/ruby /usr/bin/.


Here's what i'm using now:

every 1.day, :at => '5am' do
  # It appears that, when the following tasks are executed through
  #  cron, /usr/local/bin is not in the path (which is the ruby/RoR installation that should be used)
  #  So we need to make sure that the proper ruby/RoR installation can be found
  #  (Unfortunately this seems to restrict us to using the 'command' option, and leaves us unable
  #  to use the other options: 'runner' and 'rake' because i'm not sure how you'd affect the PATH
  #  environment variable for those)

  # (alot of what's below is just meant to provide a bit of visibility to the execution within cron)
  cmd_root = "PATH=/usr/local/bin:$PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "export PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "cd #{path}"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "pwd "
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "echo $PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "echo $RAILS_ENV"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "which ruby"

  # Execute the rake task, using 'command'
  cmd = cmd_root + " && rake clean:my:task param=val"
  command cmd

  # Without another way of manipulating the environment PATH, this isn't working right:
  #rake "clean:my:task"
end

Since i'm just using the 'command' option, it allows me to execute commands prior to the actual rake task, like adding the proper directory to the PATH so that the right ruby is used.

I'm not crazy about how this hard-codes a reference to /usr/local/bin, tho :-/. And i don't know how i could utilize the 'rake' or 'runner' options of whenever. But it seems to be working.


I had a conflict with rakes versions... I had installed it first with apt-get and then with gem... so I removed every version... I even had to remove /usr/local/bin/rake manually. Reinstalled just with gem and now it works :) Hope it helps someone.


Add bellow line of code in config/schedule.rb to set correct ruby path.

 env :PATH, ENV['PATH']

and then run bellow commands

whenever --update-crontab     #   to update crontab
service crond restart         #   to restart crontab
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜