Skip to content

Instantly share code, notes, and snippets.

@neilfws
Created August 14, 2009 03:15
Show Gist options
  • Select an option

  • Save neilfws/167620 to your computer and use it in GitHub Desktop.

Select an option

Save neilfws/167620 to your computer and use it in GitHub Desktop.
Start/stop mongod for a Rails app.
# mongodb.rake for Rails
# assumes that mongodb installed in Rails.root/lib/mongodb
namespace :db do
namespace :mongo do
# start db server
task :start do
pr = get_pids
if pr.length == 0
sh "#{Rails.root}/lib/mongodb/bin/mongod run --dbpath=#{Rails.root}/db/ >> #{Rails.root}/log/mongodb.log &"
puts "mongod running - press return."
else
puts "mongod already running - rake db:mongo:stop first."
end
end
# stop db server
task :stop do
pr = get_pids
if pr.length > 0
pr.each do |p|
begin
Process.kill(2,p)
rescue SystemCallError => e
raise e
else
puts "Killed mongod #{p} with signal 2"
end
end
else
puts "No mongod process is running."
end
end
end
end
# return array with mongod PIDs (or empty)
def get_pids
require 'sys/proctable'
include Sys
procs = Array.new
ProcTable.ps {|p|
if p.name == "mongod"
procs << p.pid
end
}
return procs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment