Retrieving PID running time

People,

I have found how to get PIDs etc but how can I find the running time of
a particular PID without messing around with a system call to ps etc

Thanks,

Phil.

···

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

That information should be in /proc/<pid>/stat. Look at the man page
for proc for more details.

···

On 12/8/05, Philip Rhoades <phil@pricom.com.au> wrote:

People,

I have found how to get PIDs etc but how can I find the running time of
a particular PID without messing around with a system call to ps etc

Yes, but what I want (in Ruby) is something like:

ptime = Process.pid.time

How can I do that?

Thanks,

Phil.

···

On Fri, 2005-12-09 at 00:03 +0900, Joe Van Dyk wrote:

On 12/8/05, Philip Rhoades <phil@pricom.com.au> wrote:
> People,
>
> I have found how to get PIDs etc but how can I find the running time of
> a particular PID without messing around with a system call to ps etc

That information should be in /proc/<pid>/stat. Look at the man page
for proc for more details.

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

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.

···

On Dec 8, 2005, at 10:14 AM, Philip Rhoades wrote:

Yes, but what I want (in Ruby) is something like:

ptime = Process.pid.time

How can I do that?

untested:

class Process
  def meth_missing pid, *args
    get_process_info(pid)[args.first]
  end

  def get_process_info pid
    # parse /proc/<pid>/stat, put values you want into some struct and return it
  end
end

···

On 12/8/05, Philip Rhoades <phil@pricom.com.au> wrote:

On Fri, 2005-12-09 at 00:03 +0900, Joe Van Dyk wrote:
> On 12/8/05, Philip Rhoades <phil@pricom.com.au> wrote:
> > People,
> >
> > I have found how to get PIDs etc but how can I find the running time of
> > a particular PID without messing around with a system call to ps etc
>
> That information should be in /proc/<pid>/stat. Look at the man page
> for proc for more details.

Yes, but what I want (in Ruby) is something like:

ptime = Process.pid.time

gwtmp01@mac.com wrote:

Yes, but what I want (in Ruby) is something like:

ptime = Process.pid.time

How can I do that?

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.

<snip>

It's not in the standard library, but there is sys-proctable, available on the RAA.

Regards,

Dan

···

On Dec 8, 2005, at 10:14 AM, Philip Rhoades wrote:

I thought Process::times just returned the system and user cpu time,
not the total real time since the application was started.

···

On 12/8/05, gwtmp01@mac.com <gwtmp01@mac.com> wrote:

On Dec 8, 2005, at 10:14 AM, Philip Rhoades wrote:
> Yes, but what I want (in Ruby) is something like:
>
> ptime = Process.pid.time
>
> How can I do that?

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.

I assumed the OP was looking for the system/user time not the clock
time.

In any case I think you'd have to go through /proc, sysctl, or
parse the output of ps to get the starting time of a process.

I only have quick access to Mac OSX where the start time is available
via the 'lstart' keyword using ps:

$ps -p3357 -o lstart
STARTED
Thu Dec 8 00:58:42 2005
$

···

On Dec 8, 2005, at 12:44 PM, Joe Van Dyk wrote:

I thought Process::times just returned the system and user cpu time,
not the total real time since the application was started.