Ruby Bundle not running via cronjob - ruby

I'm trying to run a ruby bundle from a cronjob and it's not working, yet if I run the bundle from bash command-line, it works fine. Am I missing something?
The command is:
BUNDLE_GEMFILE=/client/Gemfile bundle exec /client/bin/runner
I've set it in crontab as follows:
*/1 * * * * BUNDLE_GEMFILE=/client/Gemfile bundle exec /client/bin/runner
But am getting no results. I really appreciate the help all!
Thanks,
Justin

Little did I know rbenv was being used, so the fix is:
*/1 * * * * bash -lc 'BUNDLE_GEMFILE=/client/Gemfile bundle exec /client/bin/runner'

Related

Crontab dont find/recognize path

I'm using a osx and I created a ruby script in the path: /Users/diogo/workspace/outros/crawler_trf with name get_news.rb
So I tried to execute it via crontab with the following line: */1 * * * * 'ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb' > /tmp/crawler_trf.out and I've got the error: /bin/sh: ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb: No such file or directory
I really had searching for my answers for long but I didn't found nothing.
cron is feeding your command:
'ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb' > /tmp/crawler_trf.out
to /bin/sh as-is but there is no 'ruby /Users/diogo/...' command, there probably is a ruby command that can take /Users/diogo/... as an argument though so drop the quotes:
*/1 * * * * ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb > /tmp/crawler_trf.out
You might want to include the full path to ruby as well or at least make sure there's a PATH setting in your crontab.

Running ruby selenium scripts on Cronjob - Chrome

I am trying to run a selenium ruby script on Chrome through crontab but don't see anything run.
I tried the following as suggested in other questions:
crontab -u shamanth -e
Added the following
MAILTO=""
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export DISPLAY=:0 google-chrome
47 13 * * * /usr/bin/ruby ~/Desktop/script.rb
From the cron logs /var/log/syslog, I see that the script has started (I put some print messages in the beginning of the script) but I don't see chrome launching.
Permissions have been given to the script file.
Most likely the problem is with the path you are setting: crontab works in a very limited environment, where paths like ~ are not understood.
For that, instead of saying
* * * * * /usr/bin/ruby ~/Desktop/script.rb # NO!
# ^
say
* * * * * /usr/bin/ruby /home/your_user/Desktop/script.rb # YES!
# ^^^^^^^^^^^^^^^

Cron is running in home directory instead of file directory

I followed some other posts in stackoverflow and successfully setup cron with RVM using rvm cron setup and injected some ENV to the crontab file.
When I was troubleshooting why the dotenv gem is not working, I realised the following:
I placed my test.rb in file path /home/myuser/ruby/test.rb and had my crontab file as shown below:
* * * * * ruby /home/myuser/ruby/test.rb >> /home/myuser/ruby/output.log
and when I puts the output of the test.rb with Dir.pwd. The output states that the rb is run in the /home/myuser/ directory instead of /home/myuser/ruby directory.
While I had a hotfix by manually changing the path. But I wonder why it is the case.
By default, cron tasks of a user are executed from the user's home directory. In order to execute the script from proper directory, you have to "cd" to it.
Consider changing your crontab to:
* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log
Good luck!
According to #Pawel Dawczak who left the answer in the comment.
the solution is to rewrite the statement in crontab as
* * * * * cd /home/myuser/ruby && ruby test.rb >> /home/myuser/ruby/output.log
Thanks!

Can't get rails rake task to play nice with crontab

I currently have this shell script ...
nightly.sh
#!/bin/bash
rvm 1.9.2
cd /home/appname/capistrano/current
RAILS_ENV=production bundle exec rake nightly >> /home/appname/capistrano/shared/log/nightly.log 2>&1
I use it in my crontab entry here... crontab -e
42 20 * * * /home/appname/nightly.sh
When it runs I get this error
/home/appname/nightly.sh: line 4: bundle: command not found
I am using RVM
I've now Added some environmental variables to my crontab per #KL-7
SHELL=/bin/bash
HOME=/home/appname
PATH=/home/appname/local/bin:/home/appname/.rvm/gems/ruby-1.9.2-p290/bin:/home/appname/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/appname/.rvm/rubies/ruby-1.9.2-p290/bin:/home/appname/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Now I'm getting this...
/home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler
[minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
from /home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
from /home/appname/.rvm/gems/ruby-1.9.2-p290/bin/bundle:18:in `<main>'
It could be because its throwing an error and you are not capturing it. Try the following:
01 04 * * * /bin/bash -l -c 'cd /home/appname/capistrano/current && RAILS_ENV=production bundle exec rake nightly' >> /home/appname/capistrano/shared/log/nightly.log 2>&1
Seems like cron can't locate your bundle executable. You need to find it (using, for example, which bundle) and then either specify full path to it in crontab or set PATH environment variable at the top of crontab like that:
PATH=/bin:/usr/bin:/path/to/directory/with/bundle/
This might help:
/bin/bash -l -c
Read:
http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
Here is another solution:
* * * * * ssh localhost 'your command here...'
This will just ssh in to the same host (which sources the normal environment) and issues the command

Crontab rake job is not working with whenever gem in ruby

This is my crontab file generated by whenever:
* * * * * /bin/bash -l -c 'cd /var/apps/path && RAILS_ENV=production echo "Testing" >> /new
* * * * * /bin/bash -l -c 'cd /var/apps/path && RAILS_ENV=production rake somegroup:rake_job --silent'
The first line is for test only and works as expected. However the rake job in the other line has nothing to do at all. In /var/log/syslog, both jobs are executed. Also tried using the absolute path of rake (/usr/local/rvm/gems/ruby-1.9.2-p136/bin/rake), still no luck. Don't know where to find error message. I'm using ubuntu 10.04TS server.
Any thoughts will be appreciated! ;)
J
are you using rvm and gemsets ?
I think I've found a problem with Whenever and rvm's rvmrc file

Resources