Kill process in capistrano without pkill

Pkill has issues with capistrano, because the pkill command is always inside a capistrano command, thereby matching and killing itself.

pkill free solution

task :foo do
  kill_processes_matching "MaintenanceDaemon"
end

def kill_processes_matching(name)
  run "ps -ef | grep #{name} | grep -v grep | awk '{print $2}' | xargs kill || echo 'no process with name #{name} found'"
end

Prevent paths in mails (ActionMailer)

It happens once in a while, a path gets into a mail, but with this patch it should happen nevermore.

Looks for paths in non-production and raises if it finds one.


unless Rails.env.production?
  class ActionMailer::Base
    def render_message_with_path_protection(method_name, body)
      body = render_message_without_path_protection(method_name, body)
      if body =~ %r{((href|src)=['"]/.*)}
        raise "Use absolute path in mail urls! #{$1}"
      end
      body
    end

    alias_method_chain :render_message, :path_protection
  end
end

Hash#first for ruby 1.8.6

Just got some failing specs from our 1.8.6 test runner, when using OrderedHash#first, so we fix that…

# add {}.first if its not there (1.8.6)
unless {}.respond_to?(:first)
  class Hash
    def first
      return if keys.empty?
      [keys.first, self[keys.first]]
    end
  end
end

Remove a line from known hosts with single command

No more going into the file and deleting the entry manually.
To remove line 123 from ~/.ssh/known_hosts:

Usage

rmknownhost 123

Code
Put this it into a file called rmknownhost inside a folder that is in your PATH and chmod +x the file.

#! /usr/bin/env ruby
line = ARGV[0] || raise("gimme line to remove")
hosts = File.expand_path("~/.ssh/known_hosts")
content = File.readlines(hosts)
removed = content.delete_at line.to_i - 1
puts "Removed:\n#{removed}"
File.open(hosts, 'w'){|f| f.write content * ""}