...making Linux just a little more fun!
René Pfeiffer [lynx at luchs.at]
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.
#!/bin/sh # # Check if a printer queue is disabled and reenable it. DISABLED=3D`lpstat -t | grep disabled | awk '{ print $2; }'` for PRINTER in $DISABLED do logger "Printer $PRINTER is stopped" cupsenable -h 127.0.0.1:631 $PRINTER && logger "Printer $PRINTER has been enabled." doneThis script can be executed periodically by crontab or by any other means.
Best, René.
Thomas Adam [thomas.adam22 at gmail.com]
Hello --
On 27/12/2007, René Pfeiffer <lynx@luchs.at> wrote:
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
> for PRINTER in $DISABLED
Quotes. Use more quotes:
for PRINTER in "$DISABLED"-- Thomas Adam
Karolis Lyvens [karolisl at gmail.com]
On Thu, Dec 27, 2007 at 05:54:19PM +0000, Thomas Adam wrote:
> > 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,
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).
> > > for PRINTER in $DISABLED > > Quotes. Use more quotes: > > `` > for PRINTER in "$DISABLED" > ''
In this case, they are not relevant - word splitting is expected (and quotes prevent it from happening), because one is trying to iterate over words in the variable:
karolis@EN6350:~$ var="foo boo moo" karolis@EN6350:~$ for entry in $var; do echo "$entry"; done foo boo moo karolis@EN6350:~$ for entry in "$var"; do echo "$entry"; done foo boo mooAs you can see, when one is using quotes, then echo is called just once on the variable (and $entry is set to the entire contents of $var), which is not the desired behaviour.
-- Karolis Lyvens
Thomas Adam [thomas.adam22 at gmail.com]
On 27/12/2007, Karolis Lyvens <karolisl@gmail.com> wrote:
> In this case, they are not relevant - word splitting is expected (and > quotes prevent it from happening), because one is trying to iterate over > words in the variable:
Quite. I meant to quote PRINTER further down.
-- Thomas Adam
René Pfeiffer [lynx at luchs.at]
On Dec 27, 2007 at 1754 +0000, Thomas Adam appeared and said:
> Hello -- > On 27/12/2007, René Pfeiffer <lynx@luchs.at> wrote: > What does this return? I am assuming a tokenised list of printers > separated by whitespace?
Yes, this was the idea, and it did just that after some tests.
> Note though: > > DISABLED=3D`lpstat -t | grep disabled | awk '{ print $2; }'` > `` > DISABLED=3D"$(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.
That's a good idea. I think you just upgraded the little script to version 0.2. CUPS before 1.3.x not having a global error policy is something that bothers me more.
Best, René.