Linux – Output of ps lstart changed

linuxps

I started a program and wrote the output of ps -p PID -o lstart= to a file, like so:

$ long_running_command &
[1] 4562
$ ps -p $! -o lstart= > start_time
$ cat start_time
Wed Apr  6 06:16:31 2016

But when I re-run ps later I get a slightly different result:

$ ps -p 4562 -o lstart=
Wed Apr  6 06:16:53 2016

I was under the impression that lstart= would print the start time of a given process. Why am I getting a different start time when I re-call ps?

Best Answer

I had suspected (but hadn't been able to replicate) that the issue was somehow related to an exec call or something similar that was replacing the running process, keeping the same PID but resetting the start time. It turns out the explanation is much simpler (and unfortunately I didn't include enough detail in the original question).

As detailed in this followup, an NTP update was changing my system clock between the ps calls. Apparently lstart respects clock updates (which makes sense, but still somewhat surprised me), meaning that you can't rely on the output of lstart to remain consistent over the lifetime of a single process.

Related Question