Do not forget to buy it – if you use it for commercial purpose.
You can get it for Windows, Max, and… tadaaaa… Linux.
Monthly Archives: April 2010
unicorn start/stop and monitoring script
Today I wanted to write a script which could be used to start/stop/restart unicorn for a given application – without root privileges, to get a status and a special action(monitor) which can be called periodically and will restart unicorn automatically in case something went wrong
I found this very usefull blogpost on the net http://rubynyc.wordpress.com/2009/12/24/unicorn-scriptspin/ and modified the script with 2 additional “methods”: status and monitor. (I know … the code I added is pretty primitive… but it works)
#!/usr/local/bin/ruby
UNICORN_RAILS='/usr/local/bin/unicorn_rails'
APP_PATH="#{ARGV[0]}"
class Runner
class << self
def start
system "cd #{APP_PATH}; #{UNICORN_RAILS} -c ./config/unicorn.rb -E production -D"
end
def reload
system "kill -s USR2 #{pid}"
end
def restart
stop
start
end
def graceful_stop
system "kill -s QUIT #{pid}"
end
def stop
system "kill #{pid}"
end
def pid
File.read "#{APP_PATH}/tmp/pids/unicorn.pid"
end
def status
begin
system "ps -o user,pid,ppid,command -ax | grep #{pid}"
rescue
puts 'not started'
end
end
#start if killed
def monitor
begin
#this throws an exception in 2 cases:
#1. if no unicorn-process for the current user exists
#2. if the pid functions fails(throws an error) due to non-existant unicorn.pid-file
throw 'processes killed manually' if (`ps -o user,pid,ppid,command -ax | grep #{pid}`).split("\n").size <= 2
rescue
puts 'restarting ... '
start
end
end
end
end
case ARGV[1]
when "start"
Runner.start
when "reload"
Runner.reload
when "restart"
Runner.restart
when "stop"
Runner.stop
when "status"
Runner.status
when "monitor"
Runner.monitor
else
STDERR.puts "usage ./unicorn_script.rb absolute_path_to_RAILS_ROOT [start|stop|restart|status|monitor]"
exit(1)
end
This script can be called from a cron every minute like this
* * * * * /path/to/this_script /path/to/rails_root/ monitor
It baiscally checks if the unicorn-processes exist(the master process and his spawned kids). If it does not detect at least one unicorn-process it will calls the start routine.
howto restart networking on FreeBSD
Linux users are used to something like:
sudo /etc/init.d/networking restart
which does the entire job … usually.
On FreeBSD you have to restart the routing tables/service separately like this:
sudo /etc/rc.d/netif restart; sudo /etc/rc.d/routing restart