How is uptime calculated?

I was wondering how uptime is calculated.

Is it simply the difference between now and boot time? More specifically– If I were to boot up, run for 5 minutes, and put the machine to sleep for a year; Upon resuming, would my uptime show 5 minutes, or 1 year and 5 minutes?

I find the verbiage in the man page a little vague on this point:

uptime – Tell how long the system has been running.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

It depends on the system you are talking about. For Linux-based systems, you probably are using uptime from procps, which reads the data from /proc/uptime.

To see this, read the source-code in its Git repository, e.g., uptime.c, which uses proc/sysinfo.c.

According to CentOS documents

3.2.30. /proc/uptime

This file contains information detailing how long the system has been on since its last restart. The output of /proc/uptime is quite minimal:

350735.47 234388.90

The first number is the total number of seconds the system has been up. The second number is how much of that time the machine has spent idle, in seconds.

But the source-code in uptime.c ignores the idle value. Because the system is powered on, I would expect it to reflect the elapsed time.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply