-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup-cups-printer-enable-check.sh
113 lines (90 loc) · 3.25 KB
/
setup-cups-printer-enable-check.sh
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
#
# Enable cups printers.
# Sometimes for unknown reason cups printers disable themselfs.
# This script finds the disabled printers and re-enables them.
# or run it manualy or setup with the systemd-timers also.
#
# Code checked on Debian Bullseye
# ShellCheck - shell script analysis tool
# version: 0.7.1
# V0.1 d.d 2022-08-11 By LvB initial program setup.
# Todo, add show output of disabled/skipped printers.
### User variables.
# The location where the program is placed.
ENABLE_PRINTERS_FILE="/usr/local/sbin/cups-enable-printers.sh"
# If you want to exclude printers from the output of the program.
# Add there use | as separator. (caps independent)
# By default "idle" as i preffer not to touch working/idle printers.
ENABLE_PRINTER_EXCLUDE="idle"
# info : https://www.freedesktop.org/software/systemd/man/systemd.timer.html
# Enable The timer to run it 'after a reboot and with timer' or do it manualy.
# To enable the systemd config, set to yes.
ENABLE_SYSTEMD_TIMER="no"
# sets : OnBootSec=
CUPS_ENABLE_TIMER_DELAY="5min"
# sets : OnUnitActiveSec
CUPS_ENABLE_TIMER_ACTSEC="300"
# if yes, then the systemd Timer will be setup to run the
# cups-enable-printer.sh at boot after Xmin delay.
if [ "${ENABLE_SYSTEMD_TIMER}" = "yes" ]
then
if [ ! -f "${ENABLE_PRINTERS_FILE}" ]
then
{
echo "[Unit]
Description = Forcibly enable printer occassionally. Why CUPS disables printers in the first place has yet to be determined.
[Service]
Type = simple
ExecStart = ${ENABLE_PRINTERS_FILE}
[Install]
WantedBy = multi-user.wants"
} >> /etc/systemd/system/cups.enable.printers.service
fi
if [ ! -f /etc/systemd/system/cups.enable.printers.timer ]
then
{
echo "[Unit]
Description=Run enable printers frequently to ensure connection difficulties are remedied.
[Timer]
# Defines a timer relative to when the machine was booted up.
# OnBootSec=5min means 5 minutes after boot-up
OnBootSec=${CUPS_ENABLE_TIMER_DELAY}
# Defines a timer relative to when the unit the timer unit is activating was last activated.
# run/repeat after .. seconds.
OnUnitActiveSec=${CUPS_ENABLE_TIMER_ACTSEC}
[Install]
WantedBy = timers.target"
} >> /etc/systemd/system/cups.enable.printers.timer
fi
echo
echo "Cups Enable Times setup on systemd"
echo
echo "Now run the following commands
sudo systemctl daemon-reload
sudo systemctl enable cups.enable.printers.timer
sudo systemctl start cups.enable.printers.timer
sudo systemctl restart cups.enable.printers.timer
systemctl list-timers"
echo
fi
# Command to enable goes to the pre-set location /usr/local/sbin
if [ ! -f "${ENABLE_PRINTERS_FILE}" ]
then
{
echo "#!/bin/bash"
echo
echo "## Enable all disabled printer, (* optional with excluded filters if set.)"
echo "for PRINTERS in \$(lpstat -p|grep -iEv \"${ENABLE_PRINTER_EXCLUDE}\"|awk '{print \$2}')"
echo "do"
echo " echo \"(Re-)enabling printer: \${PRINTERS}\""
echo " cupsenable \"\${PRINTERS}\""
echo
echo "done"
} >> "${ENABLE_PRINTERS_FILE}"
# set execute rights.
chmod +x "${ENABLE_PRINTERS_FILE}"
# run it. *( enable if you like)
echo "Test the output with : lpstat -p"
echo "and run the program. : ${ENABLE_PRINTERS_FILE}"
fi