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

2 thoughts on “Kill process in capistrano without pkill

  1. Thanks! That was awesome. I was never able to figure out why “sh -c ‘pkill …’ ” didnt work. It worked with sudo though i really didnt want the kill to be run with sudo.

  2. I ran into this. However, in my case, I was matching the end of the command line used, so adding a $ to the end of my pattern caused it to match the one I wanted an not the capistrano spawned process.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s