Created
September 21, 2015 11:43
-
-
Save yuta-aoyagi/2676e015f4bd0071fcb7 to your computer and use it in GitHub Desktop.
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/ruby -WKu | |
# Find the starttime of the process pid | |
# http://qiita.com/isaoshimizu/items/ee555b99582f251bd295 | |
def starttime(pid) | |
run = proc {|cmd| | |
out = `#{cmd}` | |
raise "Failed to run: #{cmd}" if !$?.success? | |
out | |
} | |
btime = run["sed -n 's/^btime //p' /proc/stat"].chomp.to_i | |
tck = run['getconf CLK_TCK'].chomp.to_i | |
start = run["cut -d ' ' -f22 /proc/#{pid}/stat"].chomp.to_f | |
btime + start / tck | |
end | |
if $0 == __FILE__ | |
die = proc {|msg| | |
$stderr.puts msg | |
exit false | |
} | |
die['usage: starttime (pid)'] if $*.size != 1 || /\A\d+\z/ !~ (pid = $*[0]) | |
puts starttime(pid) rescue die[$!] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment