Topic: HOWTO: start p910nd when an usb printer is plugged in/switched on

Hello,

When you start or reboot your router when a printer connected to its usb port is switched off, there is no /dev/lp0 device. If you start p910nd in that case, every 10 seconds the following message is printed in the syslog:

Mar 20 08:46:17 router lpr.err p9100d[10111]: /dev/lp0: No such file or directory
Mar 20 08:46:17 router lpr.err p9100d[10111]: /dev/lp0: No such file or directory, will try opening later

I've created a little script that starts p910nd when a printer is plugged in. It uses hotplug.d. I'm not really sure how to check for a printer, but on my router (netgear wndr3700) the following works. Also, I found that p910nd seems to have a bug that the .pid file is not removed when the daemon is stopped. Requirements: usb-support (kmod-usb-core, kmod-usb-Xhci, kmod-usb2), usb-printing support (kmod-usb-printer) and p910nd. kmod-ledtrig-usbdev will turn on the usb LED automatically if the printer is switched on. See http://wiki.openwrt.org/doc/howto/p910nd.server for more info.

This is my script:

/etc/hotplug.d/usb/50-p910nd

#!/bin/sh
case "$ACTION" in
        add)
                # Check if lp device is plugged in and p910nd is not already started
                if [ ! -f /var/run/p9100d.pid -a -d /sys/devices/platform/ar71xx-ehci/usb?/*/*/*/lp0 ]; then
                        logger "USB Printer device plugged in, starting p910nd"
                        /etc/init.d/p910nd start
                fi
                ;;
        remove)
                # Check if lp device is unplugged and if p910nd is still running
                if [ ! -d /sys/devices/platform/ar71xx-ehci/usb?/*/*/*/lp0 -a -f /var/run/p9100d.pid ]; then
                        logger "USB Printer device unplugged, stopping p910nd"
                        /etc/init.d/p910nd stop
                        # p910nd does not seem to remove .pid file when stopped, removing it manually
                        rm /var/run/p9100d.pid
                fi
                ;;
esac

Improvements are welcome. Right now for example, it checks only for lp0, not lpX or other ports than 9100 (9101, 9102 etc). And the device detection could probably be better. Hope you like it.

2 (edited by larmoe 2012-09-01 08:23:44)

Re: HOWTO: start p910nd when an usb printer is plugged in/switched on

Nice script. For a while I've been using xinetd to start and stop services on demand, in an attempt to minimize memory and CPU usage.
In order to make this work, you must install xinetd, and make it start at boot.
Configure xinetd to start p910nd when a request is made to port 9100:

touch /etc/xinetd.d/p910nd

Edit /etc/xinetd.d/p910nd and make it look like this (adjust IP accordingly):

service p910nd
{
        disable         = no
        socket_type     = stream
        protocol        = tcp
        port            = 9100
        user            = root
        wait            = no
        only_from       = localhost 192.168.1.1/24
        server          = /usr/sbin/p910nd
        server_args     = -b -f /dev/lp0
}

The xinetd approach does have some advantages, if you have multiple services starting on request (e.g. sane, samba, p910nd)