diff options
Diffstat (limited to 'cmd/uptime')
| -rwxr-xr-x | cmd/uptime | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/cmd/uptime b/cmd/uptime new file mode 100755 index 0000000..fef74a8 --- /dev/null +++ b/cmd/uptime @@ -0,0 +1,29 @@ +#!/bin/sh +# @uptime - how long the xmppcb bot has been running +# +# The bot exec's this script directly, so its parent process ($PPID) is +# the xmppcb process itself; ps tells us how long that process has run. + +secs=$(ps -o etimes= -p "$PPID" 2>/dev/null | tr -cd '0-9') + +if [ -n "$secs" ]; then + d=$(( secs / 86400 )) + h=$(( secs % 86400 / 3600 )) + m=$(( secs % 3600 / 60 )) + s=$(( secs % 60 )) + if [ "$d" -gt 0 ]; then up="${d}d ${h}h ${m}m ${s}s" + elif [ "$h" -gt 0 ]; then up="${h}h ${m}m ${s}s" + elif [ "$m" -gt 0 ]; then up="${m}m ${s}s" + else up="${s}s" + fi + echo "xmppcb up: $up" +else + # fallback for a ps without 'etimes': raw elapsed string [dd-]hh:mm:ss + et=$(ps -o etime= -p "$PPID" 2>/dev/null | tr -d ' ') + if [ -n "$et" ]; then + echo "xmppcb up: $et" + else + echo "uptime: cannot read bot process (pid $PPID)" + exit 1 + fi +fi |
