Feb

25

CUPS USB printer auto-activation/deactivation

Posted by : Florian Löffler | On : 25.02.2007



CUPS glowingThe following problem has been on my mind for some time now. With my ancient SuSE 9.0 installation at my parents house printing should be possible in the following way:

1. Start the print job from some Windows box
2. walk to the currently switched off USB laser printer and switch it on
3. wait until the printer finished the job and turn it off again before you leave

Actually that doesn’t sound like too much to ask ;). But CUPS puts some rocks in this way because it tries to send a newly received print job to the printer immediately. Now if the printer is powered off at this moment CUPS changes the status of the printing queue to “stopped” making it impossible to print anything at all until somebody logs into the web interface and manually sets the state to “ready” again. What would help CUPS recognizing when the printer is powered on and automatically switch the queue’s state to “ready” again and start printing the waiting jobs. Unfortunately it doesn’t do that.

So I wrote a little script…

How it works
Possibly this problem has been fixed with later versions of CUPS but since I didn’t want to do a complete reinstall of a newer SuSE version I helped myself with a little script that monitores /var/log/messages.
Actually that script just looks out for a I-found-a-printer USB message which is matched against a regular expression and activates/deactivates the printer accordingly.

Hint: Since the script does passive polling the intervall between two polls should not be too long (slow responses) neither too short (unecessary load). Additionally you will have to adjust the regular expression to match the message unique for your USB printer but more about that later.

Setup
1. Download the auto_printer_state.sh script tp /usr/bin/auto_printer_state.sh.

2. Make the script executable by typing chmod +x /usr/bin/auto_printer_state.sh at the bash prompt.

3. Here’s the tricky part ;) Searching for messages which mark the appearance/disappearance of the USB printer is done by matching log entries against a regular expression.
Those are defined with the variables TRIGGER_ON=”" and TRIGGER_OFF=”" inside the script.
To find out what the log entries for your printer look like you can monitor your syslog while plugging in and out your usb printer with tail -f /var/log/messages.
Now you have to modify the regular expressions inside the script to match your log entries.

Hint: The construction of correct regular expressions often not trivial. If you have problems with it you should study the Wikipedia article on regular expressions here. If you’re still stuck after that you may also post a excerpt of your syslog here and I will try to make up a matching expression for you ;)

4. Well if you are using SuSE 9.0 you can replace your CUPS init script inside /etc/init.d with my modified version to automatically start (and stop) the auto_printer_state.sh script with CUPS.
Don’t forget to chmod +x /etc/init.d/cups ;)
5. OK, that should be it. Have fun! ;)

Downloads
 auto_printer_state.sh (2.74 kb)07.02.2008 21:53:04
 cups (4.69 kb)03.02.2007 10:47:36

Comments (7)

  1. [...] Edit: Das Problem mit dem automatischen deakitiever der Druckerwarteschlange sobald man den Drucker ausschaltet habe ich mit einem Skript gelöst, dass die Warteschlange abhängig vom Drucker automatisch aktiviert bzw. deaktiviert. [...]

  2. foogy said on 2007-08-29

    Hi, habe ein ähnliches Script gebastelt, dass allerdings das Spool-Verzeichnis überwacht und bei neuen Spools prüft, ob der Drucker angeschlossen ist und diesen ggf. startet. Anderer Ansatz, funktioniert aber auch:

    #! /bin/sh

    while [ true ]
    do
    # wait for some seconds to save CPU time
    sleep 10

    # check the cups spool dir for new jobs
    CHECKSPOOL=$(ls /var/spool/cups/d* | grep such)

    if [ "$CHECKSPOOL" == "" ]; then
    echo “Spool data found. Starting printer …”
    # Look for my Canon MP780. Find out your ID:VENDOR using lsusb
    CHECKPRINTER=$(lsusb -d 04a9:1707)
    if [ "$CHECKPRINTER" ]; then
    cupsenable Canon_MP780_USB_1
    sleep 60
    fi
    fi
    done

  3. flo said on 2007-08-30

    Danke für die gute Idee und das Script foogy. Deine Lösung gefällt mir fast noch besser als meine. Ist jedenfalls ressourcensparender, da nicht immer das Syslog durchgeackert werden muss.
    Meins ist dafür flexibler. Irgendwan sollte das mal so erweitert werden, dass man auf beliebige Meldungen im Syslog eine entsprechende Aktion definieren kann.
    Irgendwann… ;)

  4. harrix said on 2007-12-18

    In z.B. /etc/udev/rules.d/49-printer.rules

    SUBSYSTEM==”usb”, KERNEL==”lp*”, SYSFS{product}==”i550″, NAME=”usb/%k”, SYMLINK+=”i550″, GROUP=”lp” PROGRAM=”/usr/sbin/cupsenable tp0″

    Dann macht udevd alles allein :-))

  5. flo said on 2007-12-19

    Cool ^^
    Wusste doch, dass ich net drum rum komm mich damit mal zu beschäftigen. Weiß allerdings gar net, ob SuSE 9.0 schon udev hat. Das ist auf dem Rechner, auf dem das Skript läuft nämlich installiert :(
    Muss wohl doch mal ne ordentliche Distribution her ;)

  6. FW1704 said on 2008-12-19

    Script mit opensuse 11.1 nicht mehr nötig.
    Hab den HP Laserjet 6P nach der Deiner Anleitung
    unter VISTA eingerichtet -> läuft
    (anders, über IP Adr. wie üblich, ging’s NICHT!)

    Auch Ausschalten und wieder Einschalten geht ohne Script.

    Danke

  7. jpgorganizer said on 2008-12-24

    habe das gleiche Problem unter CUPS 1.3.9 folgendermaßen gelöst.

    * Edit cupsd.conf. Please insert the following lines at the end of the file if your currenct version of the file doesn’t have these values.

    #
    # JobRetryInterval in Sekunden
    #
    # The JobRetryInterval directive specifies the number of seconds
    # to wait before retrying a job. This is typically used for fax queues
    # but can also be used with normal print queues whose error policy
    # is retry-job.
    # The default is 30 seconds.

    JobRetryInterval 15

    #
    # JobRetryLimit
    #
    # The JobRetryLimit directive specifies the maximum number of times
    # the scheduler will try to print a job. This is typically used for
    # fax queues but can also be used with normal print queues
    # whose error policy is retry-job. The default is 5 times.
    #
    # 40 reicht aus, um Drucker in
    # 10 min einzuschalten , damit die Jobs noch gedruckt werden

    JobRetryLimit 120

    * Edit printers.conf. Change the line ErrorPolicy to ErrorPolicy retry-job

    # possible values for ErrorPoliy:
    # abort-job – Abort the job and proceed with the next job in the queue
    # retry-current-job – Retry the current job immediately
    # retry-job – Retry the job after waiting for N seconds; the cupsd.conf JobRetryInterval directive controls the value of N
    # stop-printer – Stop the printer and keep the job for future printing; this is the default value


    ErrorPolicy retry-job
    ….

    #

    Funktioniert bei mir perfekt auf einer NSLU2 mit unslung.

Leave a Reply

Your email address will not be published.