Determine system age in a shell script

One way is to grab the number of seconds from /proc/uptime, then discard everything after the decimal (since Bash can compare integers but not floating point numbers).

So instead of this:

cat /proc/uptime
400149.27 3016453.79

We get this:

sed 's/\..*//' /proc/uptime
400149

In this example, my age threshold is 1 day (86400 seconds):

if [ $(sed 's/\..*//' /proc/uptime) -gt 86400 ]; then
  echo "I'm old"
  status=old
else
  echo "I'm new"
  status=new
fi

2 Comments

  • 1. arlock replies at 30th May 2013, 11:27 am :

    uptime | grep -q day; echo $?

  • 2. arlock replies at 30th May 2013, 11:30 am :

    Or, without grep ->

    if [[ $(uptime) = *day* ]]; then echo “over a day”; fi

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).