blob: 017644e4a8eca51df9eccee7cafe918b0f2cb27f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
:
|