0

I have a script that relies in time Ubuntu 18.04 is started found by:

uptime -s

I save that value in a file when I run the script. I noticed when I started Ubuntu that time was:

uptime -s
2019-01-07 18:01:59

After running for a bit more than two hours, same command reports now a different time (I remained logged in same account all this time):

uptime -s
2019-01-07 18:01:58

I would expect the command reports same time within same login session. But somehow time changes with 1 second (it is earlier now).

Is this an OS or tool issue?

https://elixir.bootlin.com/linux/latest/source/fs/proc/uptime.c
struct timespec64 uptime;
ktime_get_boottime_ts64(&uptime);

How can I get system up time without such fluctuation?

Update:

I though I found the answer in: Linux: Getting date & time of system startup

But I was wrong, it just luck, after running a few more tests, one can see it is OS problem, the time rotates between 58 and 59 depending on nanoseconds:

$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.670091767
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.627339874
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.708332394
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.821526069
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.604129974
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.469767199
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:59.264900963
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.986611962
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.851955874
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:58.958194732
$ date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%F %T.%N"
2019-01-07 18:01:59.066728481

I seems, there is no reliable system uptime to second level.

1

1 Answer 1

0

Found an answer here:

Linux: Getting date & time of system startup

This command seems to give correct result, unlike uptime -s

 date -d "`cut -f1 -d. /proc/uptime` seconds ago"

Example:

date -d "`cut -f1 -d. /proc/uptime` seconds ago"
Mo 7. Jan 18:01:59 CET 2019

It seems uptime -s is not reliable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .