-
Notifications
You must be signed in to change notification settings - Fork 2
/
NoPrinterShutdown
executable file
·69 lines (57 loc) · 1.43 KB
/
NoPrinterShutdown
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/NoPrinterShutdown
#
# Production values
WAIT=15
SLEEP=60
USBv=03f0
USBd=1c17
# Debugging overrides
# SLEEP=1
# WAIT=2
# USBd=1c16
PIDF=/run/NoPrinterShudown.pid
if [ -e $PIDF ]
then
PIDp=$(pgrep --pidfile ${PIDF})
if [ -n "${PIDp}" ]
then
# running, so exit
exit 1
else
# not running, stale pidfile
logger -t "usb-watchdog" "stale pidfile, removing"
rm -f ${PIDF}
fi
fi
echo $$ > $PIDF
Device=$(lsusb -d $USBv:$USBd)
if [ -n "${Device}" ]
then
#We see the printer
rm -f $PIDF
exit 0
fi
# Else we don't see the printer, so we loop for 15 minutes, checking for the
# printer's return, otherwise we shut down.
logger -t "usb-watchdog" "Lost the printer, starting ${WAIT}m timer"
for timer in $(seq 1 $WAIT)
do
Device=$(lsusb -d $USBv:$USBd)
if [ -n "${Device}" ]
then
#We see the printer has returned!
logger -t "usb-watchdog" "Printer returned after ${WAIT}m. Aborting shutdown."
rm -f $PIDF
exit 0
fi
logger -t "usb-watchdog" "tic ${timer} of ${WAIT}"
sleep $SLEEP
done
# We made it this far without seeing the printer return, so just shutdown
logger -t "usb-watchdog" "Printer off-line, activating shutdown."
rm -f $PIDF
/sbin/shutdown --poweroff --no-wall now