How can I get crontab to successfully start WEBrick - ruby-on-rails

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.

Related

A server is already running

I'm currently using cloud 9 for this project. When I run it using rails server -b $IP -p $PORt it gives me this:
=> Booting Puma
=> Rails 5.0.0 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
A server is already running. Check /home/ubuntu/workspace/saasapp/tmp/pids/server.pid.
Exiting
Does anyone know how to fix this? I already tried closing and re-opening the tabs again, and stop running the run project button. The language is ruby-on-rails and I'm sort of a beginner.
The error is happening because you most likely started your server, then closed the terminal before closing the server. I also had that error, but what I did was that I clicked the memory cpu disk icon on the top right corner, then clicked restart, and re-ran my server, and it started. I'm not sure if it's the proper way, but I was following a tutorial and that's what they showed.
This happens when you close the terminal tab before ending Rails.
To end Rails, find its process ID with lsof, then kill it:
lsof -i tcp:8080
[copy the PID]
kill -9 [paste PID]
bin/rails s
Alternatively, you can also find the process ID in Puma's temporary files using cat, then kill it:
kill -9 $(cat tmp/pids/server.pid)
bin/rails s
Open this file:
/home/ubuntu/workspace/saasapp/tmp/pids/server.pid as it states in the error.
The file should contain the process ID.
Copy the ID.
Open the terminal -> https://docs.c9.io/docs/terminal and run the command
sudo kill -9 {the copied ID}
Subsequently delete the ID from the server.pid file and try to start the server again

how to auto start rails after maintenence?

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

how to stop rails server (redmine) ?

i've installed and running redmine in my domain. something went wrong and i cant access redmine admin panel. i tried to reset password and also did some google and changed password in database. but no luck still cant login. then i removed all app files but its still running same as before..
this was the code i used to run redmine server
bundle exec ruby script/rails server webrick -e -s production
now i'm trying to stop or restart but nothing is working.
is there any way to stop the server.
Thanks in advance.
Kill the process
kill -INT $(cat tmp/pids/server.pid)
Its cleaner to write a rake task to do that:
task :stopserver do
pid_file = 'tmp/pids/server.pid'
if File.file?(pid_file)
print "Shutting down WEBrick\n"
pid = File.read(pid_file).to_i
Process.kill "INT", pid
end
File.file?(pid_file) && File.delete(pid_file)
end
Delete the file tmp/pids/server.pid) and then restart the server.
Usually you'll hit Ctrl-C to stop webrick when it's started without -d option. The Ctrl-C makes INT signal, so youcould try with kill -INT <pid> to stop webrick started with -d option.
If it doesn't stop you can try with kill -9 <pid> sending a KILL signal, that's not a proper clean shutdown but seems the only way to stop it. It's not a 'best practice' but it's the only method i've ever found.
$ killall -9 ruby
this command wil kill all the running instance of ruby on your system and you can restart ur server again

Interrupt Rails Server Output w/o Stopping the Server during a Capistrano Deployment

I'm doing a Capistrano deployment of a Rails app. It's been a lot of fun for the most part.
After the deployment is complete (in deploy:restart), I would like to start the Rails server, watch the output for a while, and then hit Ctrl-C to send an interrupt, thereby stopping the output, and proceeding the deploy:cleanup task. At one point it seemed like this was working, except that it appeared to be considering the interrupt to be an exception and so was saying it "Cannot Start the Rails Server" even though it was actually started and running. I wanted to rescue the interrupt, and so wrote the following, based in part on another thread here:
namespace :deploy do
task :restart, :roles => :app, :except => { :no_release => true } do
begin
logger.info 'Attempting to Start the Rails Server'
run "cd #{release_path} && script/rails s"
rescue SystemExit, Interrupt
logger.info %q[Au revoir! And don't worry. The server will continue running just fine without you hanging around looking over it's shoulder.]
rescue Exception => error
logger.important 'Cannot Start the Rails Server. This may be a problem.'
logger.info "#{error}"
end
end
end
However, this doesn't work. Before I hit Ctrl-C, while the server is still running, as I would expect, I'm getting this sort of thing:
** [out :: server.example.com] Started GET "/assets/bootstrap.js?body=1" for 178.120.25.53 at 2012-07-09 19:10:53 +0000
** [out :: server.example.com] Served asset /bootstrap.js - 200 OK (11ms)
And then after I send the interrupt, I'm getting this:
** Au revoir! And don't worry. The server will continue running just fine without you hanging around looking over it's shoulder.
triggering after callbacks for `deploy:restart'
* executing `deploy:cleanup'
* executing "ls -xt /srv/www/my_project/releases"
servers: ["server.example.com"]
[server.example.com] executing command
command finished in 774ms
** keeping 1 of 2 deployed releases
* executing "rm -rf /srv/www/my_project/releases/20120709190209"
servers: ["server.example.com"]
[server.example.com] executing command
command finished in 811ms
Which looks right...but as it turns out, Rails is not, in fact, still running, as a grep of the processes before and after reveals.
Before Ctrl-C, I see both the Capistrano command (19358), and the Rails server it started (19507):
user#server.example.com:~$ ps ax | grep rails | grep -v grep
19358 pts/1 Ss+ 0:01 bash -c cd /srv/www/my_project/releases/20120709190521 && script/rails s
19507 pts/1 Sl+ 0:41 ruby script/rails s
After Ctrl-C, the Rails server is still there, or it appears to be:
user#server.example.com:~$ ps ax | grep rails | grep -v grep
19507 ? Sl 0:41 ruby script/rails s
But after I attempt to hit the site in a web browser, it disappears! Weird eh?
user#server.example.com:~$ ps ax | grep rails | grep -v grep
user#server.example.com:~$ [no output; returned to prompt]
So, my question is: How do I do this thing? How do sever the communication between the running Rails process and Capistrano, allow Capistrano to move on to it's remaining tasks, and then give me back my terminal prompt, without stopping the Rails server? Any help would be appreciated.
I've now realized that this was a PEBCAC error. The Begin-Rescue-End block in my Capistrano script was not catching (rescuing) my inbound Ctrl-C. It was merely passing it along to the running Rails server process, which was obediently exiting with a SystemExit, which was passed back up the line to the Capistrano script, which then caught the outbound exception. At that point it was a done deal. No amount of catching and handling the outbound exceptions from within the context of the Capistrano script was ever going to prevent the Rails server from stopping. So, I understand now why it wasn't working. But I am still curious if there's a way to do what I was trying to do. It would mean catching my inbound interrupt in Capistrano somewhere and handling it before it could be passed on to the server.
You can use Signal.trap in Ruby to catch Ctrl-C.
But I'm not sure how you can do what you need to with Capistrano – you want to spawn a grandchild process that won't be terminated when the Capistrano process is.

Using God to monitor Unicorn - Start exited with non-zero code = 1

I am working on a God script to monitor my Unicorns. I started with GitHub's examples script and have been modifying it to match my server configuration. Once God is running, commands such as god stop unicorn and god restart unicorn work just fine.
However, god start unicorn results in WARN: unicorn start command exited with non-zero code = 1. The weird part is that if I copy the start script directly from the config file, it starts right up like a brand new mustang.
This is my start command:
/usr/local/bin/unicorn_rails -c /home/my-linux-user/my-rails-app/config/unicorn.rb -E production -D
I have declared all paths as absolute in the config file. Any ideas what might be preventing this script from working?
I haven't used unicorn as an app server, but I've used god for monitoring before.
If I remember rightly when you start god and give your config file, it automatically starts whatever you've told it to watch. Unicorn is probably already running, which is why it's throwing the error.
Check this by running god status once you've started god. If that's not the case you can check on the command line what the comand's exit status is:
/usr/local/bin/unicorn_rails -c /home/my-linux-user/my-rails-app/config/unicorn.rb -E production -D;
echo $?;
that echo will print the exit status of the last command. If it's zero, the last command reported no errors. Try starting unicorn twice in a row, I expect the second time it'll return 1, because it's already running.
EDIT:
including the actual solution from comments, as this seems to be a popular response:
You can set an explicit user and group if your process requires to be run as a specific user.
God.watch do |w|
w.uid = 'root'
w.gid = 'root'
# remainder of config
end
My problem was that I never bundled as root. Here is what I did:
sudo bash
cd RAILS_ROOT
bundle
You get a warning telling you to never do this:
Don't run Bundler as root. Bundler can ask for sudo if it is needed,
and installing your bundle as root will break this application for all
non-root users on this machine.
But it was the only way I could get resque or unicorn to run with god. This was on an ec2 instance if that helps anyone.
Add the log option has helped me greatly in debugging.
God.watch do |w|
w.log = "#{RAILS_ROOT}/log/god.log"
# remainder of config
end
In the end, my bug turned out to be the start_script in God was executed in development environment. I fixed this by appending the RAILS_ENV to the start script.
start_script = "RAILS_ENV=#{ENV['RACK_ENV']} bundle exec sidekiq -P #{pid_file} -C #{config_file} -L #{log_file} -d"

Resources