Created
July 7, 2010 03:35
-
-
Save mipearson/466267 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in config/environments/production.rb | |
config.after_initialize do | |
SshTunnel.establish | |
end | |
# in lib/ssh_tunnel.rb | |
class SshTunnel | |
@@process_id = nil | |
COMMAND = "ssh [email protected] -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