Skip to content

Instantly share code, notes, and snippets.

@txag1995
Created May 2, 2014 18:39
Show Gist options
  • Save txag1995/502043118a02716025e8 to your computer and use it in GitHub Desktop.
Save txag1995/502043118a02716025e8 to your computer and use it in GitHub Desktop.
def get_chef_zero_server_pid(port)
pid = ""
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
if is_windows
process = []
stdin, stdout = Open3.popen3("netstat -ao -p TCP -n")
stdout.each_line.select do |l|
if l.split[1] =~ /:#{port}/
process.push (l.split[4]) unless process.include?(l.split[4])
end
end
process.each do |p|
stdin, stdout = Open3.popen3("tasklist /NH /FI \"PID eq #{p}\"")
stdout.each_line.select do |l|
if (l.split[0] =~ /svchost|chef-zero/)
pid = p
end
end
end
else
pid = %x[ lsof -i tcp:#{port} | grep -E 'ruby|chef-zero' | awk '{print $2}' ]
end
if pid && pid != ""
return pid
else
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment