#!/bin/bash # Version: 0.3.0-bwbar # Copyright (c) 2005-2022: # Darren 'Tadgy' Austin # Licensed under the terms of the GNU General Public License version 3. BWBAR_EXEC="/opt/bin/bwbar" DAEMON_EXEC="/usr/bin/daemon" BWBAR_ARGS=('-f' '/run/bwbar.txt' '-p' '/run/bwbar.png' '-t' '5' '-x' '800' '-y' '8' '-b' '2' '${MONINTERFACE:-eth0}' '$(( ($(ip link show "${MONINTERFACE:-eth0}" | sed -nre "s/.*qlen (.*)$/\1/ p") / 100) * 90 ))') DAEMON_ARGS=('-N' '-n' "${0##*rc.}" '-r' '-a' '60' '-A' '5' '-L' '3600' '-M' '3' '-o daemon.debug' '--') RUNUSER="$(whoami)" MONINTERFACE="eth0" # Allow configuration in /etc/default to override. # Additional available variables: # ENVIRONMENT=() # Extra environment passed to $EXEC. Must be an array. # SLAY_DELAY="" # Delay between the SIGTERM and SIGKILL on a 'stop'. Default: 2s. # RESTART_DELAY="" # Delay between stopping and starting on a 'restart'. Default: 2s. # RUNUSER="" # Username to run the daemon as. Default: User who starts the script. # MONINTERFACE="" # The interface to monitor for bandwidth usage. Default: eth0. [[ -e "/etc/default/${0##*rc.}" ]] && source "/etc/default/${0##*rc.}" error() { printf "%s: %s: %s\\n" "${BASH_SOURCE##*/}" "${BWBAR_EXEC##*/}" "$*" >&2 } checkconfigured() { # This function can be used to perform any pre-start tests; hopfully to insure the daemon # can start correctly, before actually trying to start it. A return value of 0 means the # tests were passed and the daemon should be started. Any other value prevents the # daemon from being started, and an error message will be emitted. id "${RUNUSER:-$(whoami)}" >/dev/null 2>&1 || return 1 ip link show "${MONINTERFACE:-eth0}" >/dev/null 2>&1 || return 1 return 0 } checkstatus() { local RET if su - "${RUNUSER:-$(whoami)}" -c "\"$DAEMON_EXEC\" --running -n \"${0##*rc.}\""; then printf "%s: %s: %s\\n" "${BASH_SOURCE##*/}" "${BWBAR_EXEC##*/}" "running" RET=0 else printf "%s: %s: %s\\n" "${BASH_SOURCE##*/}" "${BWBAR_EXEC##*/}" "running" RET=1 fi return $RET } startdaemon() { checkconfigured || { error "not started - pre-start checks failed" return 1 } su - "${RUNUSER:-$(whoami)}" -c "${ENVIRONMENT:+declare ${ENVIRONMENT[*]};} \"$DAEMON_EXEC\" ${DAEMON_ARGS[*]} \"$BWBAR_EXEC\" ${BWBAR_ARGS[*]}" if (( $? != 0 )); then error "error starting daemon" return 1 else return 0 fi } stopdaemon() { su - "${RUNUSER:-$(whoami)}" -c "\"$DAEMON_EXEC\" -n \"${0##*rc.}\" --stop 2>/dev/null" sleep "${SLAY_DELAY:-2}" checkstatus >/dev/null && { error "failed to stop gracefully - slaying" su - "${RUNUSER:-$(whoami)}" -c "\"$DAEMON_EXEC\" -n \"${0##*rc.}\" --signal=kill 2>/dev/null" } return 0 } for EXEC in "$BWBAR_EXEC" "$DAEMON_EXEC"; do [[ ! -e "$EXEC" ]] && { error "$EXEC:" "not found" return 1 2>/dev/null || exit 1 } [[ ! -x "$EXEC" ]] && { error "$EXEC:" "not executable" return 1 2>/dev/null || exit 1 } done case "$1" in 'start') if checkstatus >/dev/null; then error "already running" printf " %s\\n" "Try: $BASH_SOURCE status" >&2 RET=1 else startdaemon RET=$? fi ;; 'stop') if ! checkstatus >/dev/null; then error "not running" printf " %s\\n" "Try: $BASH_SOURCE status" >&2 RET=1 else stopdaemon RET=$? fi ;; 'restart') if checkstatus >/dev/null; then stopdaemon && sleep "${RESTART_DELAY:-2}" && startdaemon RET=$? else startdaemon RET=$? fi ;; 'status') checkstatus RET=$? ;; *) printf "%s\\n" "Usage: $BASH_SOURCE " >&2 RET=1 ;; esac return $RET 2>/dev/null || exit $RET