rackup server with ability to stop and restart - ruby-on-rails

i'm planning to add some tasks to my capistrano recipes file to give ability for admins to remote start/stop/restart private_pub server. But if for start i can use something like
desc "Start private_pub server"
task :start do
run "cd #{current_path};rackup private_pub.ru -s thin -E production -D"
end
i cannot find any documentation how to stop or restart rackup server. i see option
-P, --pid FILE file to store PID (default: rack.pid)
but maybe use kill command to stop server - not good idea?

I found this while searching for the same solution.
gist.github.com/3197633
basically while starting the process you have it write the pid number to a file in /tmp/pids and then when you go to stop it you read that file and it runs kill -9 ...
Hope it helps.

Your'e correct in that using kill -9 is a bad idea. This can lead to uneccesary loss of data, and as I understand it is recommended to use kill 2 or kill -INT which is equivalent to hitting 'control-c' and should close a normal server for you. I personally have started managing my servers with the God gem by TPW. Here is the script I use for running a local 'geminabox' server, for instance:
God.watch do |w|
w.name = 'gemserver'
w.dir = '/usr/local/gemserver'
w.pid_file = "#{ENV['HOME']}/.god/pids/#{w.name}.pid"
ru = File.expand_path `which rackup`
w.start = "#{ru} -D #{w.dir}/config.ru -P #{w.pid_file}"
# w.stop = lambda { Process.kill(3, `lsof -i :9292`.chomp.to_i) }
w.behavior :clean_pid_file
w.keepalive
end

Related

Monit and private_pub

to start the thin server with monit is just start program = "/etc/init.d/thin start"
but to start private_pub or faye is needed to bundle the rackup.
and how to stop the pid?
someone have a idea?
check process private_pub_myapp
with pidfile "/home/ubuntu/myapp/shared/tmp/pids/private_pub.pid"
start program = "bundle exec rackup /home/ubuntu/myapp/shared/config/private_pub.ru -s thin -E production" with timeout 90 seconds
stop program = "kill -s TERM `cat /home/ubuntu/myapp/shared/config/private_pub.yml`" with timeout 90 seconds
if totalmem is greater than 200 MB for 2 cycles then restart # eating up memory?
group private_pub_myapp
monit spitting out
/conf.d/private_pub.conf:3: Warning: Program does not exist: 'bundle'
/etc/monit/conf.d/private_pub.conf:5: Warning: Program does not exist:
'kill'
You always need to give a full, absolute path when using Monit. For example start program = "/usr/local/bin/bundle exec ..." and similar. That said, I suspect this still won't work. You seem to be trying to cat the config YAML to find the PID to kill which is probably a copy-paste issue and you'll need to make sure your rackup config is actually writing out the PID file.

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

Rails 3 delayed_job start after rebooting

I have my rails site deployed under apache. The apache is run as a service. Now I have added delayed_job there and things work fine.
Now I want to start the workers together with apache, e.g, After rebooting the server, my site and workers are up and ready so I don't have to log in and type "sudo RAILS_ENV=production script/delayed_job -n 2 start".
Another issue is that whenever I want to start the delayed_job I have to use "sudo"...
Any idea how to avoid those 2 issues?
Thanks for your help.
Use the whenever gem and its 'every :reboot' functionality. In schedule.rb:
environment = ENV['RAILS_ENV'] || 'production'
every :reboot do
command "cd #{path} && #{environment_variable}=#{environment} bin/delayed_job --pool=queue1:2, --pool=queue2,queue3:1 restart"
end
Could you just create a shell script to execute the commands you need?
#!/bin/sh
# stop delayed job
# restart apache
apachectl restart
# start delayed job
sudo RAILS_ENV=production script/delayed_job -n 2 start
It sounds like you want to have delayed_job automatically start after apache starts when you boot up the hardware. If that's the case you need to write an init script in /etc/init.d or /etc/rc.d/init.d (depending on your system). This page gives a decent primer on this:
http://www.philchen.com/2007/06/04/quick-and-dirty-how-to-write-and-init-script

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"

How to stop a Daemon Server in Rails?

I am running my rails application using the following
$script/server -d webrick
on my Ubuntu system , above command run the webrick server in background . I could kill the process using kill command
$kill pid
Does rails provide any command to stop the background running daemon server ?
like the one provided by rails to start the server , Thanks .
EDIT When it is appropriate to start the daemon server ? Any real time scenario will help Thanks
if it can be useful, on linux you can find which process is using a port (in this case 3000) you can use:
lsof -i :3000
it'll return the pid too
Like Ryan said:
the pid you want is in tmp/pids/
probably server.pid is the file you want.
You should be able to run kill -9 $(cat tmp/pids/server.pid) to bring down a daemonized server.
How about a rake task?
desc 'stop rails'
task :stop do
pid_file = 'tmp/pids/server.pid'
pid = File.read(pid_file).to_i
Process.kill 9, pid
File.delete pid_file
end
run with rake stop or sudo rake stop
The only proper way to kill the Ruby on Rails default server (which is WEBrick) is:
kill -INT $(cat tmp/pids/server.pid)
If you are running Mongrel, this is sufficient:
kill $(cat tmp/pids/server.pid)
Use kill -9 if your daemon hung. Remember the implications of kill -9 - if the data kept in Active Record caches weren't flushed to disk, you will lose your data. (As I recently did)
The process id of the daemon server is stored in your application directory tmp/pids/. You can use your standard kill process_id with the information you find there.
In your terminal to find out the process id (PID):
$ lsof -wni tcp:3000
Then, use the number in the PID column to kill the process:
$ kill -9 <PID>
pguardiario beat me to it, though his implementation is a bit dangerous since it uses SIGKILL instead of the (recommended) SIGINT. Here's a rake task I tend to import into my development projects:
lib/tasks/stopserver.rake
desc 'stop server'
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
This issues an interrupt to the server if and only if the pidfile exists. It doesn't throw unsightly errors if the server isn't running, and it notifies you if it's actually shutting the server down.
If you notice that the server doesn't want to shut down using this task, add the following line after the Process.kill "INT" line, and try to upgrade to a kernel that has this bug fixed.
Process.kill "CONT", pid
(Hat tip: jackr)
A Ruby ticket, http://bugs.ruby-lang.org/issues/4777, suggests it's a kernel (Linux) bug. They give a work around (essentially equivalent to the Ctrl-C/Ctrl-Z one), for use if you've demonized the server:
kill -INT cat tmp/pids/server.pid
kill -CONT cat tmp/pids/server.pid
This seems to cause the original INT signal to be processed, possibly allowing data flush and so on.
Run this command:
locate tmp/pids/server.pid
output:
Complete path of this file. Check your project directory name to find your concerned file if multiple files are shown in list.
Then run this command:
rm -rf [complete path of tmp/pids/server.pid file]
Here I leave a bash function which, if pasted in you .bashrc or .zshrc will alloy you do things like:
rails start # To start the server in development environment
rails start production # To start the server in production environment
rails stop # To stop the server
rails stop -9 # To stop the server sending -9 kill signal
rails restart # To restart the server in development environment
rails restart production # To restart the server in production environment
rails whatever # Will send the call to original rails command
Here it is the function:
function rails() {
if [ "$1" = "start" ]; then
if [ "$2" = "" ]; then
RENV="development"
else
RENV="$2"
fi
rails server -d -e "$RENV"
return 0
elif [ "$1" = "stop" ]; then
if [ -f tmp/pids/server.pid ]; then
kill $2 $(cat tmp/pids/server.pid)
return 0
else
echo "It seems there is no server running or you are not in a rails project root directory"
return 1
fi
elif [ "$1" = "restart" ]; then
rails stop && rails start $2
else
command rails $#
fi;
}
More information in the blog post I wrote about it.
i don't think it does if you use -d. I'd just kill the process.
In the future, just open up another terminal window instead and use the command without -d, it provides some really useful debugging output.
If this is production, use something like passenger or thin, so that they're easy to stop the processes or restart the servers
one-liner: kill -INT `ps -e | grep ruby | awk '{print $1}'`
ps -e lists every process on the system
grep ruby searches that output for the ruby process
awk passes the first argument of that output
(the pid) to kill -INT.
Try it with echo instead of kill if you just want to see the PID.
if kill process not works, then
delete file server.pid from MyRailsApp/tmp/pids/
I came here because I were trying to (unsuccesfully) stop with a normal kill, and thought I'd being doing something wrong.
A kill -9 is the only sure way to stop a ruby on rails server?
What!? Do you know the implications of this? Can be a disaster...
You can start your server in the background by adding -d to your command. For instance:
puma -d
To stop it, just kill whatever process is running on port 3000:
kill $(cat tmp/pids/server.pid)

Resources