Created
December 14, 2010 20:22
-
-
Save arkan/741027 to your computer and use it in GitHub Desktop.
This file contains 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
CURRENT_PATH = File.expand_path(File.dirname(__FILE__)) | |
def my_system(*cmd) | |
pid = fork do | |
exec(*cmd) | |
exit! 127 | |
end | |
yield pid if block_given? | |
Process.waitpid(pid) | |
$? | |
end | |
def get_pid | |
begin | |
f = File.new(File.join(CURRENT_PATH, "pid.pid"), "r") | |
pid = f.readline.gsub("\n", '').to_i | |
rescue | |
pid = -1 | |
end | |
pid > 0 ? pid : nil | |
end | |
def set_pid(pid) | |
File.open(File.join(CURRENT_PATH, "pid.pid"), "w") do |f| | |
f.puts pid | |
end | |
end | |
if pid = get_pid | |
puts "Killing process #{pid}" | |
Process.kill(9, pid) | |
File.delete(File.join(CURRENT_PATH, "pid.pid")) | |
else | |
# my_system("ssh -2 -f -c blowfish -N -C #{IP} -L 8080/127.0.0.1/80") do |pid| | |
my_system("ssh -2 -f -c blowfish -N -C #{IP} -D 8080") do |pid| | |
pid += 1 | |
puts "Pid is #{pid}" | |
set_pid(pid) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment