How to run the Ruby on Rails rake command - ruby-on-rails

I am trying to run the rake which is a ruby on rails based command, in a cron job, so I have written a bash script, and placed the bash script in the cron, but I am unable to do it.
This is the command which I have want to execute once the server boots up.
RAILS_ENV=production ruby script/delayed_job status
I bash script which is I have written which is not being executed
#!/bin/sh
export RAILS_ENV=production
/usr/bin/ruby script/delayed_job start
Kindly help me out to solve this issue

Aren't you missing a cd /path/to/your/application command?
export RAILS_ENV=production
cd /path/to/your/application && /usr/bin/ruby script/delayed_job start

Related

Running delayed job using bundle exec

In general, I will start the delayed_job using cmd:
RAILS_ENV=production bin/delayed_job start
Now I wrote a simple upstart script to automatically start the delayed job after reboot.
I found that if I wrote the cmd above in the script, it will fail.
# this is the execution in the upstart conf
exec RAILS_ENV=production bin/delayed_job start
After some google and test, I found this would work in my script:
exec bundle exec /usr/bin/env RAILS_ENV=production bin/delayed_job start
This resulted in my question, I guess /usr/bin/env RAILS_ENV=production is to pass the environment variable into the bin/delay_job script, how about bundle exec here?
Without having bundle exec in the script, it will throw error regarding to
require': cannot load such file -- bundler/setup (LoadError).
I realized that using bundle exec helps run rails under some context of the environment, but why is it necessary in the script?
Or is bundle exec necessary when I want to execute cmd like bin/delayed_job or rake in the script?
Thanks.
updated
I copied the upstart script to another node and executed it the same way.
However, it throws the incompatible library version error...
I run
bundle exec /usr/bin/env RAILS_ENV=production bin/delayed_job start
directly on terminal and it worked as expected, but failed when putting the command above in a script.
Could someone tell me why the result differed when executing the same command but in different place?

How to run a bash script to update a Rails app from outside its directory?

I have this script that should be running from the /~ directory:
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
As you can see, it pulls new commits and updates the assets and restarts Apache. The problem is when it runs the line rake assets:precompile RAILS_ENV=production, It says:
Could not find proper version of rake (12.1.0) in any of the sources
Run `bundle install` to install missing gems.
Which is strange because I am supposed to be inside the app's folder (/root/apps/monitoring) when this command is executed. What I am doing wrong?
You may need to load rvm in the script (https://rvm.io/workflow/scripting) and may be select proper ruby/gemset.
Also you can consider using wrapper for bundle created with rvm wrapper
Please try
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
bundle exec rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
With bundle exec it should work.
I run rake tasks using bash script wrappers. The trick is to use the source command to load in the rvm environment after the task is started
example.sh
#!/bin/bash
# change the directory
cd /home/ubuntu/GSeries
# load rvm ruby
source /home/ubuntu/.rvm/environments/ruby-2.4.2#mygemset
bundle exec rake db:prune_logs RAILS_ENV="production" &>> /home/ubuntu/GSeries/log/prune_logs.log

bundle exec rake doesn't run in cron

I did some researching but all cron and bundle exec related didn't cover the problem I am having so excuse me again if this had been discussed.
I am running Ubuntu 13.10 and have a Ruby On Rails app that have few rake tasks that needs to be run on Cron every few minutes or so.
I run a whenever gem, with the help of which, this syntax
every 3.minutes do
rake 'update_balance'
end
transforms to this line in crontab file
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
And when I copy this line exactly
/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
and run in it console, it runs perfectly fine and updates several records in database, as supposed.
But when set to cron, I can see it running in /var/log/syslog file, but nothing is really executed.
May 13 13:06:01 sandbox2 CRON[9656]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
May 13 13:06:01 sandbox2 CRON[9655]: (CRON) info (No MTA installed, discarding output)
May 13 13:09:01 sandbox2 CRON[9789]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
Even when I add &>/tmp/mycommand.log to crontab command, every next launch of cron command just completely truncates this file, however again, If I launch it manually, it works nicely and leaves me with this output.
2014-05-13T11:11:25Z 10292 TID-2asbo INFO: Sidekiq client with redis options {:url=>"redis://127.0.0.1"}
Sent task for updating 2 users
Any help with this issue is much appreciated.
Thanks.
I've encountered problems like this before due to the fact that cron runs as a different user. With rake in particular, i have to use the full path to rake since the cron user doesn't have the right folder in it's PATH.
So, my cron lines for rake tasks look like this:
30 8 * * 1 cd /ebs/www/apps/myproject/www && /usr/local/bin/rake mailer:send_weekly_expiring_users_reminder RAILS_ENV=production

Turn off rvm greeting on login

I have problem with foreman and rvm - foreman generates start and stop init.d scripts for resque worker.
The problem is that start script tries to write pid of new worker in file, but writes it with rvm message Using /home/dev/.rvm/gems/ruby-2.0.0-p247 that appears on login.
Of course, I may edit generated script to cut off that part, but then I'll need to do that every time scripts are regenerated.
Here is how foreman tries to save pid:
su - $USERNAME -c 'cd /home/path/to/current; export PORT=5000; bundle exec rake environment resque:work QUEUE=* RAILS_ENV=production >> /var/log/app_name/job-1.log 2>&1 & echo $!' > $PIDDIR/job.1.pid
How can I disable that rvm message? Or any other solutions?
you are using cd to load ruby, the Using message is most likely coming from it, the ad hook solution would be to silence it:
cd /home/path/to/current >/dev/null
there is also a flag to silence the message, but it might not work with older .rvmrc files:
echo rvm_use_flag=0 >> ~/.rvmrc

Could not find rake on capistrano deploy

So I am attempting to get a capistrano deployment setup with an nginx/unicorn server, using RVM.
The deploy works, but when I attempt to interact with unicorn via capistrano i get an error like:
[~/source/quibbler/config]
$ cap unicorn:start
* executing `unicorn:start'
* executing "cd /u/apps/quibbler/current && bundle exec unicorn_rails -c /u/apps/quibbler/current/config/unicorn-production.rb -E production -D"
servers: ["wwwtestvm.whf.app"]
[wwwtestvm.whf.app] executing command
** [out :: wwwtestvm.whf.app] Could not find rake-0.9.2 in any of the sources
command finished in 553ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'ruby-1.9.2-p290#quibbler' -c 'cd /u/apps/quibbler/current && bundle exec unicorn_rails -c /u/apps/quibbler/current/config/unicorn-production.rb -E production -D'" on wwwtestvm.whf.app
I have searched the interwebs all night and can not find the solution. If I copy paste the command into my shell it works so I am 99% sure it has to do with the RVM environment not being correct.
I have created a gist of my deploy and unicorn configs:
https://gist.github.com/1375736#file_deploy.rb
https://gist.github.com/1375736#file_unicorn_production.rb
Any help or guidance would be appreciated.
Thanks to sannankhalid answer on this other question i was able to resolve it.
Short answer: add rake to your Gemfile, make sure you run bundle install so your Gemfile.lock gets updated, then deploy to your server, ayay!

Resources