2-cent tip: Automatically reenabling CUPS printer queues

René Pfeiffer [lynx at luchs.at]

Thu, 27 Dec 2007 16:05:23 +0100

Hello!

I have a short shell script fragment for you. It automatically reenables
a printer queue on a CUPS printing server. CUPS takes different actions
when a print job encounters a problem. The print server can be
configured to follow the error policy "abort-job", "retry-job" or
"stop-printer". The default setting is "stop-printer". The reason for
this is not to drop print jobs or to send them to a printer that is not
responding. Beginning with CUPS 1.3.x you can set a server-wide error
policy. CUPS servers with version 1.2.x or 1.1.x cna only have a
per-printer setting.

If you have a CUPS server an wish the print queue to resume operation
automatically after they have been stopped, you can use a little shell
script to scan for disabled printers (stopped printing queues) and
reenable them.

What does this return? I am assuming a tokenised list of printers
separated by whitespace? (It would have to be since you've not
modified IFS anywhere...) Note though:

> DISABLED=`lpstat -t | grep disabled | awk '{ print $2; }'`

DISABLED="$(lpstat -t | awk '/disabled/ { print $2 }')"

I've always preferred writing such things where one makes -use- of
their tools at hand, especially allowing awk to conditionally do the
matching. No need for grep here. I also like $() for command
substituion, and before anyone goes off on portability, I'll
compromise with you: $() is nestable unlike backticks, and if you
have a shell old enough not to understand $() you don't deserve to be
using it in the first place. :P

Technically, backticks are nestable, but the nesting is extremely ugly,
especially after 2 levels or so:

echo "`echo \`echo word \\\`echo foo \\\\\\\`echo bar\\\\\\\`\\\`\``"

> and if you have a shell old enough not to understand $() you don't
> deserve to be using it in the first place. :P

In other words, $() is defined in POSIX, and, while there are shells that
predate this standard, it is portable to every modern UNIX-like system
(except Solaris, where /bin/sh is still pre-POSIX - I might be wrong,
though).