how to auto start rails after maintenence? - ruby-on-rails

i host my rails application on cloud server,
my server is using centos 6,
and what i am trying to achieve is,
i want everytime server is restarted, i need my rails app to do an auto start.
i tried crontab, but somehow i can't get it working,
here is my crontab content :
#reboot cd ~/UAAdmin/urbanacecode && /usr/local/rvm/gems/ruby-2.2.4/bin/bundle exec rails server -d >> deploy_log.log 2>&1 &
as you can see, i dump my crontab result to log file,
somehow it's not working?
here is my dump result :
/usr/bin/env: ruby_executable_hooks: No such file or directory
this is very strange, because when i tried to run my crontab commands manually, i can see the server running.
i ran this manually
cd ~/UAAdmin/urbanacecode
then
/usr/local/rvm/gems/ruby-2.2.4/bin/bundle exec rails server -d
what should i do to get it working? please advice,
oh btw i am pretty new with linux OS, so kindly give an example
how to do it properly.
many thanks..

Related

Shell commands fail in Rails in production mode

I have deployed on a VPS a Rails 4 application that executes shell command depending on the tasks. The app runs on Nginx with Passenger.
The situation I am having is that none of the shell commands work. After a bit of debugging, I've noticed that every command returns a "not found" error message.
For example:
out = %x[date 2>&1] # outputs 'sh: 1: date: not found\n'
why is this happening? is there some configuration setting I should set to allow shell commands in production mode? I am quite new to rails so I'm not really sure what could be causing this.
UPDATE:
Here's a bit more information, when querying from rails whoami, i get www-data, if I print the $PATH I get /usr/local/lib/ruby/gems/2.1.0/bin but then if in the command line execute sudo -u www-data printenv PATH the result is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin... Does this makes any sense?

Unable to see error in test.log in rails

I am having problem to generate a log to see the error of page break in log/test.log
Everything is generating except page break issue.
I have a project running on aws
when I start puma server using
bundle exec puma -e production -d -b unix:///tmp/run/pokerstop.sock
and generate log with tail -f log/production.log commands
I can see my production log
but when I start my puma sever in test environment and check log using
tail -f log/test.log command nothing comes out
sometime page goes break on server and I want check cause of page break in test server but nothing comes out
Please help me
Thanks.
Do you expect your rails app to be run in a test environment like development and production..?
If so, run your rails server in test environment as follows:
rails s -e test
You should see the logs of your application in /log/test.log file.
Also, make sure you have added database configuration for test environment in database.yml file.
Hope this helps you.!!

EC2 : Rails deployment gives blank pages

I have deployed my rails application on EC2. It runs on two servers. One for rails application and second for DB.
When I start application using "rails s -e production&" and if I stay connected using SSH,
I can see the webpages.
As soon as I disconnect SSH I can not see the pages.
There are no errors thrown. One weird thing is "Production.log" file does not have anything.
everything is spit out on console.
You are running rails in the current ssh session. Any programs you have running during that session will stop if you disconnect. You need to set up your rails app to run as a daemon using something like Phusion Passenger.
You are basically running the built in WEBrick server that is not really meant for production so it's likely that the process is getting killed after the parent process (your ssh process) gets terminated.
You can probably tweak the configuration to make WEBrick not quit, or you can simply run your session using screen or tmux
Screen:
$ screen
$ rails s -e production &
$ screen -d
When you want to reattach:
$ screen -r
Tmux:
$ tmux
$ rails s -e production &
$ # Hit <ctrl-b><ctrl-d> to detach
When you want to reattach:
$ screen attach -t 0
Or like #datasage mentioned you can run your Rails with an actual production web server like Passenger Phusion or Unicorn.

Run a command in the context of a different app

I have two rails apps on the same server, let's call them A and B.
I am trying to have app A restart app B via app B's own capistrano task. Unfortunately, even after cd-ing to app B's directory, it is trying to run app A's capistrano instead. Am I missing something?
example code
system("cd /apps/appB/current && pwd && bundle exec cap:restart")
pwd correctly returns the path of appB (/apps/appB/current), however, in there is a traceback for cap:restart. This is because it is still trying to run the cap command in the context of appA, e.g.
/apps/appA/shared/bundle/ruby/1.9.1/gems/capistrano-2.15.4/lib/capistrano/configuration/loading.rb:152:in 'require': cannot load such file -- airbrake/capistrano (LoadError)
from /apps/appA/shared/bundle/ruby/1.9.1/gems/capistrano-2.15.4/lib/capistrano/configuration/loading.rb:152:in 'require'.
I tried without 'bundle exec', and have tried some other ways of making system calls. I also created a bash script in another directory and tried to run it that way.
All methods described exhibit the same behaviour.
Your help would be greatly appreciated =)
You need to use Bundler.with_clean_env to ensure that your subprocess doesn't pick up your current Bundler environment:
Bundler.with_clean_env do
system("cd /apps/appB/current && pwd && bundle exec cap:restart")
end
This is essentially the same problem as Install bundle of gem within other rails application
Since you said your apps are using Unicorn, you can signal from app A to app B (or the other way around).
Read this page: http://unicorn.bogomips.org/SIGNALS.html
The only thing each application needs to know is the pidfile path of the other application. So look at your Unicorn config and see where it's storing that.
You could either read the PID out of that pidfile and kill it from Ruby:
pid = File.read(path_to_other_application_pidfile).chop
Process.kill("USR2", pid)
Or you could use backticks to execute shell commands
`kill -s USR2 \`cat #{path_to_other_application_pidfile}\``

How can I get crontab to successfully start WEBrick

I am trying to run an hourly cron job that pulls some code, starts rails and executes some tests (the code pulling and tests don't really matter for this, as they work fine).
Here's the crontab line from my user (i'm on Ubuntu 10.04):
0 */1 * * * /home/me/src/dev-setup/scripts/hourly_ui_test_cron_script
and here's the script itself (minus the comments i echo to myself, the git and test stuff):
#!/bin/bash
USER=me
HOME=/home/$USER/
DISPLAY=:0
source $HOME/.bashrc
source $HOME/.rvm/scripts/rvm
PATH=/home/$USER/.rvm/gems/ruby-1.9.2-p290/bin:/home/$USER/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/$USER/.rvm/rubies/ruby-1.9.2-p290/bin:/home/$USER/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
cd $HOME/src/project
pgrep -f ruby | xargs kill -9
rails s -e test >> hourly_test.log
and this mostly works, except that my WEBrick starts and immediately exits with no error; here's the log:
=> Booting WEBrick
=> Rails 3.1.3 application starting in test on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
and there's nothing registered in the rails log. I've tried running just rails s, i've tried running the script from root's crontab; nothing works (the script runs if i just execute it from command line).
Has anyone seen this?
TIA
Could you try adding -d to script which will daemonise. See what happens with that or do you need running in foreground?
Seems a very strange thing to do though generally so I hope you've got good reason to be using webbrick in this way.

Resources