Linux: Finding out uptime of the system by reading ∕proc∕uptime

📄 Wiki page | 🕑 Last updated: Feb 19, 2023

Every running Linux system exposes its uptime through the textual file in the virtual /proc filesystem. Although we can easily find out the uptime of the system by using tools like uptime or who, this method does have some advantages, especially in scripting.

This info is exposed in form of the textual file called uptime, whose contents we can read simply by using cat:

cat /proc/uptime

You should see something like this:

70627597.92 70553325.72

The first value indicates the number of seconds since the system has been up.

The second value indicates the number of seconds each core spent idle since the system has been up (this means that on systems with multiple cores, this number can be bigger than the first one).

We're usually interested in the first number, so let's use awk to extract it and convert it into minutes:

awk '{print int($1/60);}' /proc/uptime

A tutorial on how awk works is beyond the scope of this article, but to put it simply, in this command, awk will split the contents of /proc/uptime using the default whitespace separator, then we're taking the first argument ($1), dividing it by 60, and calling int() function on the result.

In general, awk is a very useful tool for these types of manipulations and definitely worth learning.

Similarly, to get the number of days, we can divide the number by 86400:

awk '{print int($1/86400);}' /proc/uptime

Result:

817

My test system has uptime of 817 days, and we can compare this number with the output of uptime command:

 21:53:34 up 817 days, 10:45 ....

The numbers should always match as uptime command uses the same information that's provided by the kernel in /proc/uptime.


Ask me anything / Suggestions

If you have any suggestions or questions (related to this or any other topic), feel free to contact me. ℹ️


If you find this site useful in any way, please consider supporting it.