diff options
Diffstat (limited to 'kvm-user-daemon.sh')
-rwxr-xr-x | kvm-user-daemon.sh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/kvm-user-daemon.sh b/kvm-user-daemon.sh new file mode 100755 index 0000000..017644e --- /dev/null +++ b/kvm-user-daemon.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# File: /usr/local/bin/kvm-user-daemon.sh +# Location: vm2 +# Author: bgstack15 +# Startdate: 2023-01-11-4 11:37 +# Title: KVM user daemon +# Project: kvm-mapping +# Purpose: daemon that runs inside user session and runs a script when specific USB device is plugged in +# History: +# Usage: +# Run at start of session, such as in ~/.fluxbox/startup +# Reference: +# original design (at least my third time) of stdout/stderr timestamps and logging +# Improve: +# Dependencies: +# Documentation: +test -z "${LOGFILE}" && test -d ~/log && LOGFILE=~/log/kvm +test -z "${LOGFILE}" && test -d ~/.log && LOGFILE=~/.log/kvm +test -z "${LOGFILE}" && export LOGFILE=/tmp/kvm-log +KVMFILE=/run/kvm/kvmfile +STOPFILE=/tmp/stop-kvm # touch this file to stop the daemons for all users +# The user should set RUNSCRIPT, which is what should happen when the kvm is switched to this output. +test -z "${RUNSCRIPT}" && RUNSCRIPT=~/bin/kvm-plugged-in.sh +! test -x "${RUNSCRIPT}" && RUNSCRIPT=/usr/local/bin/kvm-plugged-in.sh +# Crazy nesting allows stdout, stderr prepended with timestamps and "STDERR", both to normal streams and to the logfile. +{ + { + { + # Safety valve. When this file exists, the loop stops. + while ! test -f "${STOPFILE}" ; + do + sleep 1 + test -f "${KVMFILE}" && { + #env + #echo "sample error" 1>&2 + echo "kvm input detected, running \"${RUNSCRIPT}\"" + "${RUNSCRIPT}" + # delay a few seconds, because switching inputs fastis unlikely and not recommended, and so that any other user daemons looking for it can find it and also react. + { sleep 3 ; rm -f "${KVMFILE}" ; } + } + done + } 2>&3 1>&4 + } 3>&1 1>&4 | plecho "STDERR:" | tee -a "${LOGFILE}" 1>&2 +} 4>&1 | plecho | tee -a "${LOGFILE}" +# Delay a few seconds so other daemons can also find it and stop +sleep 3 ; rm "${STOPFILE}" 1>/dev/null 2>&1 +# exit 0 with this nop +: |