How to get the start time of a long-running Linux process?

后端 未结 6 1414
醉梦人生
醉梦人生 2020-12-07 06:49

Is it possible to get the start time of an old running process? It seems that ps will report the date (not the time) if it wasn\'t started today, and only the y

6条回答
  •  有刺的猬
    2020-12-07 07:22

    As a follow-up to Adam Matan's answer, the /proc/ directory's time stamp as such is not necessarily directly useful, but you can use

    awk -v RS=')' 'END{print $20}' /proc/12345/stat
    

    to get the start time in clock ticks since system boot.1

    This is a slightly tricky unit to use; see also convert jiffies to seconds for details.

    awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=$1; next }
        END { printf "%9.0f\n", now - ($20/ticks) }' /proc/uptime RS=')' /proc/12345/stat
    

    This should give you seconds, which you can pass to strftime() to get a (human-readable, or otherwise) timestamp.

    awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=$1; next }
        END { print strftime("%c", systime() - (now-($20/ticks))) }' /proc/uptime RS=')' /proc/12345/stat
    

    Updated with some fixes from Stephane Chazelas in the comments; thanks as always!

    If you only have Mawk, maybe try

    awk -v ticks="$(getconf CLK_TCK)" -v epoch="$(date +%s)" '
      NR==1 { now=$1; next }
      END { printf "%9.0f\n", epoch - (now-($20/ticks)) }' /proc/uptime RS=')' /proc/12345/stat |
    xargs -i date -d @{}
    

    1 man proc; search for starttime.

提交回复
热议问题