Skip to content

Instantly share code, notes, and snippets.

@andyjeffries
Created August 19, 2011 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyjeffries/1156553 to your computer and use it in GitHub Desktop.
Save andyjeffries/1156553 to your computer and use it in GitHub Desktop.
Process Grepper and Killer for Mac OS X
#!/usr/bin/env ruby
kill = false
name = ARGV[0]
if (name == "-k")
kill = true
name = ARGV[1]
end
if name
ps = `ps axwu|grep -i [#{name[0..0]}]#{name[1..1000]}|grep -v " #{Process.pid}"`
if (ps == "")
puts "No processes found"
else
puts ps
if kill
ps.split("\n").each do |line|
pid = line[/.*?(\d+)/,1].to_i
if pid
puts "Killing #{pid}"
`kill -9 #{pid}`
end
end
end
end
else
puts "usage: psg [-k] name"
puts " Grep for a process name excluding the grep process"
puts " -k Kill all matching processes"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment