Skip to content

Instantly share code, notes, and snippets.

@anselmobd
Last active August 11, 2022 23:39
Show Gist options
  • Save anselmobd/8a3b4eca0dcebe581ec8a7f756383ce0 to your computer and use it in GitHub Desktop.
Save anselmobd/8a3b4eca0dcebe581ec8a7f756383ce0 to your computer and use it in GitHub Desktop.
Better uptime to log (compact and standardized)
#!/bin/bash
if [ ! -f $0.pkgchkok ] ; then
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' bc 2> /dev/null |grep "install ok installed")
if [ "" = "$PKG_OK" ] ; then
echo "Required 'bc' package not installed"
exit 1
else
touch $0.pkgchkok
fi
fi
DATE="$(date +"%y/%m/%d %a %H:%M:%S")"
UPTIME="$(echo "scale=0
upsecs=$(cat /proc/uptime | cut -d" " -f1)/1
updays=upsecs/60/60/24
updaysecs=upsecs-updays*60*60*24
uphours=updaysecs/60/60
upmins=updaysecs/60-uphours*60
print updays
print \"D \"
print uphours
print \"H \"
print upmins
print \"M\"
" | bc)"
USERS="$(who | wc -l)"
LA="$(cat /proc/loadavg | cut -d" " -f-3)"
echo "${DATE} ${UPTIME} ${USERS}U ${LA}"
@anselmobd
Copy link
Author

Required utility: (not installed by default on minimal Debian)

  • bc command

This script output samples:

21/11/09 ter 19:00:14 61D 3H 33M 1U 0.54 0.11 0.26
21/11/09 ter 19:30:46 0D 8H 35M 1U 0.41 0.39 0.53

"uptime" command output samples:

19:00:14 up 61 days,  3:33,  1 user,  load average: 0,54, 0,11, 0,26
19:30:46 up  8:35,  1 user,  load average: 0,41, 0,39, 0,53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment