uptime 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh -e
  2. #
  3. # uptime: condensed uptime of the machine
  4. #
  5. # Copyright (C) 2009 Raphaël Pinson.
  6. # Copyright (C) 2009 Canonical Ltd.
  7. # Copyright (C) 2011-2014 Dustin Kirkland
  8. #
  9. # Authors: Raphaël Pinson <raphink@ubuntu.com>
  10. # Dustin Kirkland <kirkland@byobu.org>
  11. #
  12. # This program is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation, version 3 of the License.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. __uptime_detail() {
  24. uptime
  25. }
  26. __uptime() {
  27. local u= idle= str=
  28. if [ -r /proc/uptime ]; then
  29. read u idle < /proc/uptime
  30. u=${u%.*}
  31. elif [ -x /usr/sbin/sysctl ]; then
  32. # MacOS support
  33. u=$(/usr/sbin/sysctl -n kern.boottime | cut -f4 -d' ' | cut -d',' -f1)
  34. u=$(($(date +%s) - $u))
  35. fi
  36. if [ "$u" ]; then
  37. if [ "$u" -gt 86400 ]; then
  38. str="$(($u / 86400))d$((($u % 86400) / 3600))h"
  39. elif [ "$u" -gt 3600 ]; then
  40. str="$(($u / 3600))h$((($u % 3600) / 60))m"
  41. elif [ "$u" -gt 60 ]; then
  42. str="$(($u / 60))m"
  43. else
  44. str="${u}s"
  45. fi
  46. else
  47. # Last ditch hack
  48. str=$(uptime | sed -e "s/.* up *//" -e "s/ *days, */d/" -e "s/:/h/" -e "s/,.*/m/")
  49. fi
  50. [ -n "$str" ] || return
  51. color w b; printf "%s" "${str}"; color --
  52. }
  53. # vi: syntax=sh ts=4 noexpandtab