1

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 file 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?

ilasno
  • 714
  • 1
  • 13
  • 31

4 Answers4

5

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/.

Greg Fairbrother
  • 1,041
  • 8
  • 13
  • Right, makes sense - thanks. But how do i make an adjustment like that for the user that the cron job is running as? Because right now that just looks like: every 10.minutes do rake "clean:my:task" end and i don't really see how to adjust the path in that context.Should it be done in the whenever setup somehow, or should it be done in the linux environment? – ilasno Dec 15 '10 at 19:00
  • since your rake task is looking for /usr/bin/ruby and it does not exist, doing "sudo ln -s /usr/local/bin/ruby /usr/bin/." will create a link from your actual ruby in /usr/local/bin/ to /usr/bin/ so rake should then find it no matter the user. – Greg Fairbrother Dec 21 '10 at 05:31
  • This seems like the best choice, although i'm not the administrator of the server so i'm not going to use this at this point. Thanks! – ilasno Dec 21 '10 at 19:35
1

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.

ilasno
  • 714
  • 1
  • 13
  • 31
0

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
Satishakumar Awati
  • 3,604
  • 1
  • 29
  • 50
0

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.

Carlos
  • 1
  • 1
  • I can second this approach worked for me. Seems like a cleaner way than symlinks. –  Sep 14 '12 at 00:33