Jeremy Jones wrote: > Hello World! > > I'm looking for a way to check a Processes age (how long it's been > running). Is there a way to do this without installing extra modules? > I know ps-ef does it, I was hoping for elegance over the hammer & > chisel... >
one way of doing that is to interface the /proc process table if you don't want to install extra modules. here is a simple example of what you can do to get the running time of your script: #!/usr/bin/perl -w use strict; #-- #-- ask the process table to give me my own process info #-- open(PROC,'/proc/self/stat') || die("error but why? $!\n"); while(1){ #-- #-- generate some CPU activity #-- my $p; $p = sqrt($$) * $_ for(1..300000); #-- #-- gets the process time so far. more on that later #-- my $time = (split(/\s+/,<PROC>))[13] * 0.01 / 100; #-- #-- process ID and the how long it has been running in user mode #-- print "$$: $time\n"; #-- #-- rewind and get ready for the next read #-- seek(PROC,0,0); #-- #-- just to avoid printing too much too fast to the screen #-- sleep(1); } close(PROC); __END__ prints: 2845: 0.0065 2845: 0.013 2845: 0.0194 2845: 0.0259 2845: 0.0323 2845: 0.0387 ... ^C note: * open /proc/self/stat simply ask the kernel for information about the current process that's running. 'self' points to its own process and 'stat' gives you information about the running process. there are tons of information that 'stat' returns but we will be concentrated on just one. * this line: my $time = (split(/\s+/,<PROC>))[13] * 0.01 / 100; asks the kernel to give you updated information about the current process (in this case, it's our own process) that's running. we split the line into chunks and the 14th element gives us the number of jiffies that this process and its children have been scheduled in user mode. i don't know how familiar you are with the kernel (or kernel programming) but a jiffies is basically 100 millisconds for most platform (except for Alpha (which sets it to 1024, i believe) and Real Time Linux (which sets it to 100,000)). the '* 0.01 / 100' simply converts it to seconds. this, of course, is not very accurate becauser you can simply change the jiffies on your system and recompile your kernel. the split on white space is also bugous but for this example, let's assume it's correct. for more, check out 'man proc' * if you run the above script and 'top -p 2845' side by side at the same time, you will notice they both report very similiar result. this is no surprise because 'top' (as well as 'ps' and Proc::ProcssTable) is implemented using the same '/proc' process table with similar technique. * the above script is very efficient because it directly goes to the process table for the info it needs thus avoid using a shell (such as parsing the output of 'ps' or 'top') or walking the whole process table (such as Proc::ProcessTable). david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]