Add printers using shell script

Chuey
Contributor III

Hello All,

Thank you in advance for reading. I've read a few different posts on here about adding printers using a script and before I did so I came up with my own to add about 50-60 printers to a bunch of machines. It is as follows:

#!/bin/bash


# Remove any currently installed printers

lpstat -p | awk '{print $2}' | while read printer

    do

        echo "Deleting Printer:" $printer

        lpadmin -x $printer

    done


# Add Printers  

/usr/sbin/lpadmin -p QUEUENAME -o printer-is-shared="False" -E -v lpd://10.x.x.x -P /Library/Printers/PPDs/Contents/Resources/HP LaserJet 2420.gz -D "Room Number"

    echo "Added Room 1"

/usr/sbin/lpadmin -p QUEUENAME2 -o printer-is-shared="False" -E -v lpd://10.x.x.x -P /Library/Printers/PPDs/Contents/Resources/HP LaserJet 2420.gz -D "Room Number"

    echo "Added Room 2"

and so on until the last printer is added

Everything works fine, I just noticed that it'll start adding them really fast and then slow down and sometimes I get an "lpadmin: Undefined error: 0"

It'll continue to add them no problem I was just wondering if this was caused by the amount of printers I was adding at once that kind of "bogs" it down and it's nothing to worry about or if I could script it better to run more efficiently.

Any suggestions are appreciated.

Thank you

7 REPLIES 7

RobertHammen
Valued Contributor II

What if you put a sleep x command in your loop (where x = number of seconds you want it to pause)? That would tell you if you're running into some kind of timing issue.

Try this on a Mac with an SSD? Still see the slowdown? Nothing much else going on this system?

pblake
Contributor II

Could it be you get an error if the machine has no printers already to remove?

Look
Valued Contributor III

I'm with @RobertHammen put a sleep 1 after each printer add.
It's probably just kicking off some post add task in the background each time and then your trying to add another printer well before it's finished and then another and another and so on...

davidacland
Honored Contributor II
Honored Contributor II

Are all of these printers being added to all of the Macs? If you are it might be expected that there are some failures.

Could you just scope the relevant printers to their corresponding rooms?

Longer term you might be better off with a pull printing system so you only have to deploy one queue.

Chuey
Contributor III

Thanks for your responses.

@RobertHammen I will try the sleep command, this will be pushed out to mainly Macbook airs with flash storage. I tested on mine which also has flash storage and had no other programs running. Like i said it would complete but error out then continue adding them. Almost like it timed out.

@pblake Issue was adding printers, it would start off adding them fast then slow down and almost time out and give the error but then resume adding

@davidacland Yes, these printers are being added to all macs, they are mobile labs so they could be anywhere in the building which is why all printers are needed.

Thanks for your input, I'll try the sleep command and report back!

RobertHammen
Valued Contributor II

Also, be aware that lpd may not be the best way to configure these printers. JetDirect (socket://) and/or IPP (ipp://) may be better ways to configure these, depending on how new the printers are and what support the vendor has for these protocols. lpd = the lowest common denominator, and configuring printers via lpd typically does not allow the Mac to query the printers...

Chuey
Contributor III

Update: I actually found that I was running my parallels desktop which I think caused some hiccups when I was testing..smh..

@RobertHammen I'll look into the other protocols, I was just adding them how our organization has been adding them. They are old printers, HP LaserJet 2420dn. I have not had any issues using lpd and seems to work fine for us, way more reliable than "bonjour".

We're really trying to phase these printers out. Just got all new copiers setup with a Pharos Print Server system which makes it as easy as printing to a general queue, swiping your badge at any copier in any building, and bam theres your print job which has been working great but people have been so use to having their own printers.