Skip to content

Instantly share code, notes, and snippets.

@rocky
Created November 13, 2014 14:40
Show Gist options
  • Save rocky/1157eee27193b1099e0f to your computer and use it in GitHub Desktop.
Save rocky/1157eee27193b1099e0f to your computer and use it in GitHub Desktop.
Unix pidof for OSX
#!/usr/bin/env ruby
# pidof for OSX. Basically a small modification of doitian's gist
target = ARGV[0] || ""
ids = []
`/bin/ps -ax -o pid,comm`.split("\n").each do |line|
pid, command = line.chomp.split(' ', 2)
short_command = File.basename(command)
if target == command or target == short_command or
target =~ /^[-]?#{short_command}/
ids << pid
end
end
exit 1 if ids.empty?
puts ids.join(' ')
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment