Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuta-aoyagi/2676e015f4bd0071fcb7 to your computer and use it in GitHub Desktop.
Save yuta-aoyagi/2676e015f4bd0071fcb7 to your computer and use it in GitHub Desktop.
#!/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