Created
November 13, 2014 14:40
-
-
Save rocky/1157eee27193b1099e0f to your computer and use it in GitHub Desktop.
Unix pidof for OSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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