43

How can I see when a process started, assuming I know the pid. (On Linux)

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253

6 Answers6

58

If you want only the start time, you can select the field and suppress the header by doing this:

 ps -p YOURPID -o lstart=

the output will look like this:

 Mon Dec 14 17:17:16 2009

which is ctime(3) format and you can parse it to split out the relevant parts.

Other start fields such as start, stime, bsdstart and start_time age the time (after 24 hours only the date is shown, for example).

You can, however, use them directly for recently started processes without further parsing:

ps -p YOURPID -o stime=

which would output something like:

09:26
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
9

awk '{print $22}' /proc/$pid/stat - gives you the start time in jiffies after boot

James
  • 7,643
  • 2
  • 24
  • 33
  • Beautifully obscure answer! – wzzrd Dec 18 '09 at 10:23
  • Riddle me this. A system with an uptime of '17:57' has a process with a start time of '727975'. Looks like the process started 8 days from now? – Scott Pack Dec 18 '09 at 13:25
  • 1
    It's actually in jiffies (100/sec) – MarkR Dec 18 '09 at 14:32
  • 1
    Way too obscure! And besides, now you have to look up the boot time and do the math to convert jiffies to seconds and calculate the offset to get clock time. Easy, but too many steps. See Chopper3's answer. – Dennis Williamson Dec 18 '09 at 15:08
  • 1
    The amount of jiffies per second is stored in system variable HZ. It is mostly 100. To calculate it in shell you might use this: https://stackoverflow.com/a/44524937/1950345 – reichhart Jun 24 '17 at 09:39
7

"ps -f" - it's in the man pages

Chopper3
  • 101,299
  • 9
  • 108
  • 239
0

Following Dennis Williamson's excellent answer, the ps command also has the -O option which, according to the man page: is "Like -o, but preloaded with some default columns." This allows you to grep for the command (program) associated with the PID, if you don't know the PID itself.

Example: finding when an apt-get process hanging on Debian/Ubuntu started:

ps -A -O lstart= | grep apt-get | grep -v grep

Piping to grep -v grep filters out lines containing the string "grep", which removes the command we just typed in (since we don't want it).

On my system right now, this gives:

1461407  Apr 15 06:00:00 2021 S ?        00:05:09 apt-get autoremove -y
freeB
  • 1
0

If there's a single process with a given name (e.g. openvpn) on the host, you can do:

ps -p `pgrep openvpn` -o lstart=
Dan Dascalescu
  • 601
  • 1
  • 10
  • 21
Alex
  • 71
  • 1
  • 2
-2

one way you can ps -f |grep <pid> as you said you the pid otherwise you can see in top also

Axel Beckert
  • 398
  • 2
  • 17
Rajat
  • 3,349
  • 22
  • 29
  • 1
    This will match far more things than planned. `ps` has the `--pid` option to pass directly a specific pid you are interested in. – Patrick Mevzek Jan 11 '20 at 00:31