Skip to content

Instantly share code, notes, and snippets.

@mipearson
Created July 7, 2010 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mipearson/466267 to your computer and use it in GitHub Desktop.
Save mipearson/466267 to your computer and use it in GitHub Desktop.
# in config/environments/production.rb
config.after_initialize do
SshTunnel.establish
end
# in lib/ssh_tunnel.rb
class SshTunnel
@@process_id = nil
COMMAND = "ssh mpearson@redacted.locationB -R 9988:localhost:3000 -N -T -o PasswordAuthentication=no"
def self.establish
if running?
STDERR.puts "SSH tunnel already running, not running."
else
@@process_id = Process.fork { exec COMMAND }
Process.detach(@@process_id)
Kernel.at_exit do
kill
end
end
end
def self.running?
@@process_id or `ps -ax | grep '#{COMMAND}' | grep -v 'grep' | wc -l`.chomp.to_i > 0
end
def self.kill
if @@process_id
Process.kill 'TERM', @@process_id
@@process_id = nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment