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].


In reply to Re: Get Process Start Time by ambrus
in thread Get Process Start Time by Saved

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.