Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Get Process Start Time

by Saved (Beadle)
on Feb 16, 2011 at 19:24 UTC ( [id://888553]=perlquestion: print w/replies, xml ) Need Help??

Saved has asked for the wisdom of the Perl Monks concerning the following question:

I wish to give a process Number, and get the start time in seconds from epoc. I have been looking through the internet, but still need help please. How can I do that in perl. OS: I need to monitor some HP-UX, and some RHEL-Linux Purpose: to monitor root access left logged in too long. Plan: Compare now to process start time Method: Get both times into epoch, and compare. I am trying to use "Proc::ProcessTable::Process" Could anyone give me an example of it's use? Thanx

Replies are listed 'Best First'.
Re: Get Process Start Time
by ikegami (Patriarch) on Feb 16, 2011 at 20:06 UTC
Re: Get Process Start Time
by fidesachates (Monk) on Feb 16, 2011 at 21:00 UTC
    Going to agree with the first poster. Doesn't sound like you tried very hard. However, I recently read a post that reminded us (monks in general) that we shouldn't force newbies to go through the pain we did.

    In an effort to help, but not give you the answer, have you considered that perl isn't the best solution for this? If you're on a *nix system, a ps command with the right switches and grep filters can produce what you need. The only thing you'd need is to change the time to epoch time. Look at system(). This is the "easiest" and "simplest" method, but there are much better solutions that don't rely on the underlying system commands.

    If you need a pure perl solution because you're on a windows systems(even then, there's tasklist), you're in pursuit of better practices, or this is a homework assignment, see the posts by my fellow monks above.
Re: Get Process Start Time
by ww (Archbishop) on Feb 16, 2011 at 19:44 UTC
Re: Get Process Start Time
by wind (Priest) on Feb 16, 2011 at 19:59 UTC

    perldoc: perlvar - Search for $PID or $$

    perldoc: time

    Not what you need most likely, but gotta be with the above poster in asking what you have tried thus far?

Re: Get Process Start Time
by hsinclai (Deacon) on Feb 16, 2011 at 20:11 UTC
    Definitely try something, then post.. here is one idea
    use strict; use warnings; my $launch_time = time(); # Do something with $launch_time # execute external my $result = qx!/some/program -flags!; # Check for success # or #exec and exit { exec ('/some/program') }; print STDERR "couldn't exec program: $!"; # if exec fails, this script continues $launch_time = 'LAUNCH_FAILED';
    On *nix try perldoc -q time to see more ideas.
Re: Get Process Start Time
by ambrus (Abbot) on Feb 17, 2011 at 16:38 UTC

    As fidesachates suggested, if you are on Linux, you could try parsing the time output of ps like this.

    perl -we 'use Date::Manip::Date; my $pid = shift; my $sstart = readpip +e("TZ=UTC ps --no-headers --format lstart --pid " . (0+$pid)); my $st +art = Date::Manip::Date->new($sstart, [SetDate => "zone,UTC"]); my $e +start = $start->printf("%s"); print "process $pid started at epoch ti +me $estart\n";' 1967

    Alternately let's skip running ps and emulate what it does (except that it actually obtains the Hertz value in a different way).

    perl -we 'use POSIX(); my $pid = shift; my $seconds_since_boot; { open + my $UPTIME, "<", "/proc/uptime" or die; <$UPTIME> =~ /(\S+)/ or die; + $seconds_since_boot = 0+$1; } my $start_time; { open my $STAT, "<", +"/proc/" . (0+$pid) . "/stat" or die; <$STAT> =~ /\)(.+)/ or die; $st +art_time = 0+(split " ", $1)[19]; } my $Hertz = POSIX::sysconf(POSIX: +:_SC_CLK_TCK()); my $start_epoch = time() - $seconds_since_boot + $st +art_time / $Hertz; print "process $pid started at epoch time $start_e +poch\n"' 1967

    Note that this may have up to one second of error.

    One would think this problem has an easier solution, and maybe someone else will show one, but I couldn't get anything simpler than this.

    Update 2012-04-13: a somewhat related later question is A regular expression that will work for (hh:)mm:ss [converting clock time to seconds].

Re: Get Process Start Time
by Anonymous Monk on Apr 16, 2023 at 18:34 UTC
    In any POSIX compliant system, if you wish to know the number of seconds that a process have been around, you can use the ps command:
    pid=12345 ps -o etime= "$pid"
Re: Get Process Start Time
by 0xbeef (Hermit) on Feb 17, 2011 at 21:15 UTC
    Did you consider using the TMOUT environment setting? The default shell from HP-UX 10.0 is sh-posix (OSF POSIX), and TMOUT can be used to disconnect idle login sessions. This should also work in earlier versions of HP-UX that make use of ksh.

    You would simply need to set this environment variable system-wide (in /etc/profile) or in the user's individual profile file to cause an idle login session to be terminated after a specified amount of seconds:

    # Set TMOUT to 15 minutes, read-only so user cannot override it. TMOUT=900 readonly TMOUT export TMOUT

    Niël

Re: Get Process Start Time
by Anonymous Monk on Feb 18, 2011 at 17:06 UTC
    Given that purpose is to "monitor root access left logged in too long" you might want to have a look at Sys:Utmp maybe.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://888553]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found