#!/bin/sh # /proc/uptime gives something like 1234.45 12345.67 # We truncate at the first decimal to get the integer number of up seconds seconds=$(sed -e 's/\..*//' < /proc/uptime) hours=$(( seconds / 3600 )) echo hours=$hours
Programming Tips - Linux: bash script to get number of hours a machine has been up
Date: 2024jun27
Language: bash
Q. Linux: bash script to get number of hours a machine has been up
A. Rather than parsing the output of the `uptime` command
we get the info from /proc/uptime.
Its in seconds. So get hours we divide by 3600 (the number of seconds
in an hour).