Every Linux user knows (or must know) the
uptime command. But not everyone knows the details this command outputs, its use cases, or situations when this command could be valuable.
It is one of the essential Linux commands when dealing with system administration. So let's explore its benefits.
The output might have slight differences between machines, but typically it prints something like this:
12:42 up 4:02, 2 users, load averages: 2.92 3.06 2.68
The following is a breakdown of what the above output means:
|12:42||System Time from the server clock|
|up 4:02||How long system is up (4 hours and 2 minutes up)|
|2 users||How many users are active on the system now|
|2.92 3.06 2.68||Load average information for last-minute, 5, and 15 minutes.|
This column with load average might be a bit confusing. Still, the easiest way to understand that would be to think that numbers represent how many of your CPUs are working on 100% usage.
If your machine has 1 CPU, the load average will be between 0.00 and 1.00. Where 1.00 represents 100% CPU usage.
Things change when the machine has multiple CPUs. For example, in 4 CPU machine, this number could go up to 4.00.
But numbers could go higher than CPU count.
In that case, CPUs are working at 100% processing speed, and there is a queue of processor work waiting in the queue. So your machine should start "lagging" at this point.
Adding more cores (CPUs) will accelerate your server performance.
It is physically hard to do when talking about a stand-alone computer. Almost impossible to do with laptop machines.
But when talking about servers - usually server providers can upgrade/downgrade machines quite quickly as they do virtualization.
Adding more CPUs will cost additional money, sometimes a significant dedicated hosting plan upgrade.
Killing processes is only a temporary solution as you risk ending up in a high load situation again.
With the command
top -o cpu you can find a sorted list of processes eating most of your CPU.
Better processes forking could help you in this case for a long-term solution. Like using fewer processes or threads for your apps or webservers.
Below seventy percent (70%) for one CPU core should give stable machine output.
For one CPU max would be 0.7. For 4 CPUs machine, that would be around 3.0).
You now know what the uptime command does – time to explore its use cases.
The first use case will be to check if the connection to the server works smoothly without entering into the machine. With the help of this command:
$ ssh username@hostname uptime 13:03:59 up 6 days, 21:28, 0 users, load average: 0.48, 0.56, 0.52
It will print uptime output from the
hostname server and close the connection afterward.
You can wrap the entire command with the
time command and see how long it takes to connect to the server:
$ time ssh username@hostname uptime ssh hostname uptime 0.03s user 0.00s system 2% cpu 1.547 total
It is more efficient than the
ping command, as ping to the server does not indicate that it is operational. For example, it might be that the network connection to the server is functional, but the server inside it - is not.
One reason for this case could be the High Load Average issue we just mentioned.
On macOS machines
uptime command does not offer any customizable options.
On Linux machines entering
uptime -p will give you pretty mode, which will skip load average information (will display only uptime time in human-readable format):
up 6 days, 21 hours, 35 minutes
To learn when the system was booted (turned on), enter the `uptime -s' command:
You can also enter help mode and see all options possible with
uptime -h, but there will be nothing else to show.
uptime command alone, you can learn if the system was recently rebooted or if you have a CPU load problem.
Suppose you care about running a successful online business. In that case, it is crucial to measure all these things as it might lead to SEO penalties or lost customers.
With UptimeTea, you can monitor your servers and CPU load and be notified when the server got rebooted or did not respond. Try it out!