<HTML ><HEAD ><TITLE >Uptime</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Bash Prompt HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Prompt Code Snippets" HREF="c679.html"><LINK REL="PREVIOUS" TITLE="Load" HREF="x746.html"><LINK REL="NEXT" TITLE="Number of Processes" HREF="x771.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Bash Prompt HOWTO: </TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x746.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 11. Prompt Code Snippets</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x771.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN758" ></A >11.8. Uptime</H1 ><P >As with load, the data available through <B CLASS="COMMAND" >uptime</B > is very difficult to parse. Again, if you have the <TT CLASS="FILENAME" >/proc/</TT > filesystem, take advantage of it. I wrote the following code to output just the time the system has been up: </P ><TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" >#!/bin/bash # # upt - show just the system uptime, days, hours, and minutes let upSeconds="$(cat /proc/uptime) && echo ${temp%%.*})" let secs=$((${upSeconds}%60)) let mins=$((${upSeconds}/60%60)) let hours=$((${upSeconds}/3600%24)) let days=$((${upSeconds}/86400)) if [ "${days}" -ne "0" ] then echo -n "${days}d" fi echo -n "${hours}h${mins}m"</PRE ></FONT ></TD ></TR ></TABLE ><P >Output looks like "1h31m" if the system has been up less than a day, or "14d17h3m" if it has been up more than a day. You can massage the output to look the way you want it to. This evolved after an e-mail discussion with David Osolkowski, who gave me some ideas. </P ><P >Before I wrote that script, I had a couple emails with David O, who said <SPAN CLASS="QUOTE" >"me and a couple guys got on irc and started hacking with sed and got this: <B CLASS="COMMAND" >uptime | sed -e 's/.* \(.* days,\)\? \(.*:..,\) .*/\1 \2/' -e's/,//g' -e 's/ days/d/' -e 's/ up //'</B >. It's ugly, and doesn't use regex nearly as well as it should, but it works. It's pretty slow on a P75, though, so I removed it."</SPAN > Considering how much <B CLASS="COMMAND" >uptime</B > output varies depending on how long a system has been up, I was impressed they managed as well as they did. You can use this on systems without <TT CLASS="FILENAME" >/proc/</TT > filesystem, but as he says, it may be slow. </P ><P >Relative speed: the "upt" script takes about 0.68 seconds on an unloaded 486SX25 (half that as a function). Contrary to David's guess, his use of sed to parse the output of "uptime" takes only 0.22 seconds. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x746.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x771.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Load</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c679.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Number of Processes</TD ></TR ></TABLE ></DIV ></BODY ></HTML >