Skip to content

Instantly share code, notes, and snippets.

@carlzulauf
Last active December 17, 2015 01:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlzulauf/5527048 to your computer and use it in GitHub Desktop.
Save carlzulauf/5527048 to your computer and use it in GitHub Desktop.
Thin initd script. Spawns thin daemons from a config directory using a specific non-privileged user (`carl`).
#!/usr/bin/env ruby
# thin init script
# install to /etc/init.d/thin
# crazy debian LSB header stuff
### BEGIN INIT INFO
# Provides: thin
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: thin initscript
# Description: thin
### END INIT INFO
CONFIG = "/home/carl/www/thin"
BUNDLE = "/home/carl/.rvm/gems/ruby-2.0.0-p0@global/bin/bundle"
USER = "carl"
def call_thin(action = "start")
Dir[File.join(CONFIG, "*.yml")].each do |config|
dir=`awk '/^chdir:/ { print $2; }' #{config}`.chomp
cmd = "cd #{dir} && #{BUNDLE} exec thin #{action} -C #{config}"
wrapper = "su -l #{USER} -c '#{cmd}'"
puts "Executing:\n#{wrapper}"
system wrapper
end
end
case ARGV.first
when 'start'
call_thin "start"
when 'stop'
call_thin "stop"
when 'restart'
call_thin "restart"
else
puts "usage: $0 start|stop|restart"
end
@alassek
Copy link

alassek commented May 6, 2013

I can't believe I never thought of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment