This sort of thing is very platform specific. I’m assuming
a unix/linux/macosx environment. I can’t answer for Windows.
For your own process you can use Process::times, which
probably ends up calling getrusage(), a kernel system
call. See the Ruby doc for Process::times, and your system
docs for getrusage.
For the general problem of finding the information for
an arbitrary process it becomes more difficult. Some unix
systems make that sort of information available via the
/proc file system. Some make it available via the sysctl
system calls.
In either case, I don’t think there is a standard Ruby library
for parsing /proc or for extracting sysctl values. You would
have to role your own.
Another hurdle is /proc and sysctl often restrict access to
programs with root privileges. So you would have to create
a setuid root ruby script to be able to read /proc or sysctl
information directly.
So if you want to get the information for an arbitrary process,
I think the quick and dirty solution is to capture the output of
ps and parse it yourself. You can use
text = ps -l -p1234
to get the data and then string juggling to extract the time info.
For your own process you can use Process::times, which
probably ends up calling getrusage(), a kernel system
call. See the Ruby doc for Process::times, and your system
docs for getrusage.
I thought Process::times just returned the system and user cpu time,
not the total real time since the application was started.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.