Skip to content

Instantly share code, notes, and snippets.

@hamstar
Created September 14, 2012 14:28
Show Gist options
  • Select an option

  • Save hamstar/3722236 to your computer and use it in GitHub Desktop.

Select an option

Save hamstar/3722236 to your computer and use it in GitHub Desktop.
init file for running rails apps as a service (in screen) - redmine is example
#!/usr/bin/ruby
app_name="redmine"
pid_file="/var/run/#{app_name}.pid"
user="redmine"
appdir="/home/#{user}/redmine"
LOAD_RVM="source /home/#{user}/.rvm/scripts/rvm"
START_APP="RAILS_ENV=production passenger start -a 127.0.0.1 -p 3002"
STOP_APP="RAILS_ENV=production passenger stop -a 127.0.0.1 -p 3002"
def start
if File.exists pid_file
pid=File.read(pid_file).chomp
puts "#{app_name} already running on pid #{pid}"
puts "If it is not, delete #{pid_file} and try to start again"
exit
end
pid = fork do
File.chdir appdir
system "su #{user} -c \"#{LOAD_RVM} && #{START_APP}\" 2>&1 > /dev/null"
exit
end
Process.detach pid
File.open(pid_file, "w") do |f|
f.write pid
end
end
def stop
if File.exists pid_file
pid=File.read(pid_file).chomp
File.chdir appdir
system "su #{user} -c \"#{LOAD_RVM} && #{STOP_APP}\" 2>&1 > /dev/null"
Process.kill pid
puts "#{app_name} stopped"
File.delete pid_file
else
puts "#{app_name} is not running"
end
end
def status
if File.exists pid_file
pid=File.read(pid_file).chomp
puts "#{app_name} running on pid #{pid}"
else
puts "#{app_name} not running"
end
end
case ARGV[0]
when "start"
start
when "stop"
stop
when "restart"
stop
start
when "status"
status
default
puts "Usage: /etc/init.d/#{app_name} {start|stop|restart|status}"
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment