Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Correctly grep and display the uptime, load average and amount of users

Published: 06-09-2012 | Author: Remy van Elst | Text only version of this article


❗ This post is over eleven years old. It may no longer be up to date. Opinions may have changed.

Table of Contents


While doing my monitoring script ray-mon I found out it can be quite hard to get the correct values for uptime, load average and users on different systems.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

For example, the uptime command displays this on a mac:

 8:27  up 1 day, 17:06, 1 user, load averages: 1,01 0,87 0,79

This on a debian 6 machine:

 06:27:33 up 4 days,  2:36,  2 users,  load average: 0.16, 0.26, 0.27

This on a RHEL 5 machine:

 06:28:26 up 54 min,  127 users,  load average: 6.34, 6.28, 6.27    

Small differences in space placement, "load averages:" instead of "load average:" and "min" vs "days". It took me quite a while to find out how to get the correct values for the following items using just unix tools:

  • Uptime
  • Amount of users logged in
  • Current load average

Uptime

uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $2" "$3 }'

First take the output of uptime to grep, only show matching characters (-oh) and do regex (-e). Filter on "up" "space" "everything after that". Then go to sed to remove the comma's "4 days," to "4 days", and then with awk print the second and third collumn, and remove the rest of the output.

Users logged in

uptime | grep -ohe '[0-9.*] user[s,]'

Take the output of uptime to grep, only show matching characters (-oh), and do regex (-e). Filter on "number between 0 and 9" "any amount of the previous character" "space" "user, or users"

Load

uptime | grep -ohe 'load average[s:][: ].*' | awk '{ print $3 }'

First take the output of uptime to grep. only show matching characters (-oh), and do regex (-e). Filter on "load average: or load averages:" "everything after that". The with awk print the 3d collumn.

Tags: awk , bash , grep , load , snippets , uptime , users