Automating CUPS with shell commands

Print on Command

A Raspberry Pi is an ideal print server on a small network with the CUPS printing system controlled by a fully automated shell script.

The Raspberry Pi lends itself well for use as a small print server, connecting printers to a number of computers on a network. The classic software solution for Linux print servers is the Common Unix Printing System (CUPS), which replaces less convenient printing services from the Linux and Unix environment, like lpd and lprng. Although many Linux users control CUPS in a web interface or with GUI programs, most of the steps involved in both of these approaches can be fully automated with a shell script.

To install CUPS, enter the commands:

$ sudo apt update
$ sudo apt dist-upgrade
$ sudo apt install cups printer-driver-gutenprint

Postscript Printer Description (PPD) files are the basis of the service. These files contain printer properties, such as resolution, available colors, varieties of paper, formats, margins, and trays. CUPS comes equipped with PPD descriptions for widely distributed printer models. If in doubt, you should use the PPD supplied with the device by the printer manufacturer. Most manufacturers offer PPDs for their products that are also suitable for use under Apple OS X.

The working principle behind CUPS is fairly simple. Applications send their print jobs to the CUPS scheduler, which manages the queues, making sure the jobs are performed sequentially. The manager accomplishes this by routing the print data to a converter, or filter, that converts the data into the postscript (PS) format. The PS files generated in this manner then go to the PS printer, another CUPS server, or the back end. If necessary, the back end converts the PS data into a format that can be processed by a non-PS printer.

Printers and Queues

When not used with a graphical user interface, CUPS commands differ from the classical tools. The descriptions in this article distinguish between printers as devices and print queues, although when shell commands are used, they are usually synonymous. However, it is possible to set up multiple print queues for a particular printer, which can help when you have a printer equipped with additional paper trays for different types of paper.

Basically, print administration comprises a collection of small programs, each of which fulfills a particular function. Table 1 lists the programs and their respective functions. Adjusting the settings on the print server always requires administrative rights. Therefore, you should log in to the system as root or execute commands with sudo.

Table 1

CUPS Programs

Command

Function

lpadmin

Configure print queue

lpinfo

List supported devices

lpoptions

List printer settings and set them

lpstat

Request printer status

lpc

Request printer status (CUPS version)

cupsctl

Configure settings for cupsd, the scheduler

cupsaccept

Accept print jobs

cupsreject

Reject print jobs

cupsenable

Start printer queue

cupsdisable

Stop printer queue

cupsaddsmb

Export printers to Samba for Windows clients

Working Ahead

The first step is to copy all available PPD files to the /etc/cups/ppd directory for the printer that you will be controlling. You should have at hand all relevant information about the printer. This would include type of interface (USB/serial/parallel), IP address, hostname if this is used, exact model label, extensions that have been installed (e.g., for a second paper tray), additional trays, memory extensions, font ROMs, and so on. When a network printer is involved, you should also determine which protocol and ports the printer uses. In case of doubt, Nmap can scan your ports for printers that have been powered up (Figure 1).

Figure 1: The Nmap port scanner offers valuable assistance when a user wants to determine which ports and protocols are used by a printer.

In this example, I have a somewhat old Kyocera FS-2000. The output shows that it uses the Internet Printing Protocol (IPP) on port 631. The scan also recognized the JetDirect protocol developed by HP on open ports 9100 to 9103. The printer also supports the line printer daemon protocol used by LPD and LPRng on port 515.

To connect the printer via the USB port, you should first connect it to a power source, turn it on, and then execute the command lpinfo -v. As part of this process, you find the device URI (Figure 2, sixth line of output), which is important in later steps. To ensure effective communication with the printer, you should formulate its URI carefully (see examples in Table 2). When using a Raspberry Pi as a print server, you should enter its hostname or IP address.

Table 2

Printer URIs

Interface

Specification

Example

USB

usb://<manufacturer>/<model>?<serial number>

usb://Kyocera/FS-1370DN?serial=Q660918195

 

usb:/dev/usb/lp<Number>

usb:/dev/usb/lp0

Network LPD

lpd://<Hostname>/<queue>

lpd://fs2000/lp1

 

lpd://<IP Address>/<queue>

lpd://192.168.0.53/lp1

Network socket

socket://<Hostname>/<Port>

socket://fs2000:9100

 

socket://<IP Address>/<Port>

socket://192.168.0.53:9100

Network IPP

ipp://<Hostname>:631

ipp://fs2000:631

 

ipp://<IP Address>:631

ipp://192.168.0.53:631

Figure 2: The lpinfo -v command starts the query for all printers that are physically connected to the computer. In the example shown, the printer is a Kyocera FS-1370DN on the USB port.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content