Book: LPI Linux Certification in a Nutshell
Section: Chapter 16.  Text-Editing, Processing, and Printing (Topic 1.7)



16.2 Objective 2: Manage Printers and Print Queues

Printing documents is a slow and error-prone process. Printers accept data in small amounts; they run out of paper, jam, and go offline for other reasons. Printers also must accept requests from multiple system users. As a result, by design, the end user is isolated from printing functions on most computer systems. This isolation comes in the form of a print queue, which holds print requests until the printer is ready for them. It also manages the order in which print jobs are processed.

Many Unix and Linux systems use printing utilities developed for Berkeley Unix, commonly known as lpd. This objective describes printer management using lpd. The companion commands lpr, lpq, and lprm areSUID programs, which run with privileges of the superuser.[3] This is necessary to allow their use by all users, because they manipulate files in the protected print spooling directories. lpr is covered in Section 16.3. lpd, filters, spool directories, and /etc/printcap is covered in Section 16.4.

[3] SUID means "set user ID" root; see Section 4.5.

16.2.1 Linux Printing Overview

On Linux, the default printing system is derived from a standard developed for Unix systems. It consists of the following elements:

lpd

The lpd daemon is started at boot time and runs constantly, listening for print requests directed at multiple printers. When a job is submitted to a print queue, lpd forks a copy of itself to handle jobs on that queue. The copy exits when the queue is emptied. Thus, during idle periods, one lpd process will be running on your system. When printing is active, one additional lpd process will be running for each active queue.

/etc/printcap

The printcap file (short for "printer capabilities") contains printer names, parameters, and rules; it is used by lpd when spooling print jobs. See Objective 4 for additional information on the /etc/printcap file.

lpr

The lpr (short for "line print") program submits both files and information piped to its standard input to print queues.

lpq

The lpq program queries and displays the status and contents of print queues.

lprm

lprm removes print jobs from print queues.

lpc

The superuser administers print queues with lpc (line printer control).

Filters

When a printer lacks the ability to directly render a print job, software filters are used to transform the origin data into something the printer can handle. A common example is the conversion from PostScript to PCL for laser printers without native PostScript capability.

Spool directories

The lpd daemon uses /var/spool/lpd for the spooling of data awaiting printing. This directory contains a subdirectory for each printer configured on the system (both local and remote). For example, the default locally attached printer on most Linux systems is simply called lp (for "line printer"), and all of its control files and queued jobs are stored in directory /var/spool/lpd/lp.

Print jobs

Each submitted print request is spooled to a queue and assigned a unique number. The numbered print jobs can be examined and manipulated as needed.

16.2.2 Managing Print Queues

As a system administrator, you'll be asked to manage and manipulate printer queues more often than you'd like. On Linux, the lpq, lprm, and lpc commands are your tools.

lpq

Syntax

lpq [options] [users] [ job#s]

Description

Query a print queue. If numeric job#s are included, only those jobs are listed. If users are listed, only jobs submitted by those users are listed.

Options

-l

Long output format. This option results in a multiline display for each print job.

-Pname

This specifies the print queue name. In the absence of -P, the default printer is queried.

Example 1

Examine active jobs:

$ lpq
lp is ready and printing
Rank   Owner      Job  Files                 Total Size
active root       193  filter                9443 bytes
1st    root       194  resume.txt            11024 bytes
2nd    root       196  (standard input)      18998 bytes

Here, filter is currently being printed. resume.txt is up next, followed by the 18,998 bytes of data that was piped into lpr's standard input.

Example 2

Examine queue lp, which turns out to be empty:

$ lpq -Plp
no entries

Example 3

Examine those same jobs using the long format:

$ lpq -l
lp is ready and printing

root: active                             [job 193AsJRzIt]
        filter                           9443 bytes

root: 1st                                [job 194AMj9lo9]
        resume.txt                       11024 bytes

root: 2nd                                [job 196A6rUGu5]
        (standard input)                 18998 bytes

Example 4

Examine jobs owned by bsmith:

$ lpq bsmith
Rank   Owner      Job  Files                 Total Size
7th    bsmith     202  .bash_history         1263 bytes
9th    bsmith     204  .bash_profile         5676 bytes

Using the job numbers reported by lpq, any user may remove her own print jobs from the queue, or the superuser may remove any job.

lprm

Syntax

lprm [-Pname] [users] [job#s]
lprm -

Description

Remove jobs from a print queue. In the first form, remove jobs from queue name or from the default queue if -P is omitted. If users or job#s are specified, only those jobs will be removed. In the second form, all of a normal user's jobs will be omitted; for the superuser, the queue will be emptied.

Example 1

As a normal user, remove all of your print jobs:

$ lprm -

Example 2

As the superuser, remove all jobs from queue ps:

# lprm -Pps -

You may occasionally be surprised to see a no entries response from lpq, despite observing that the printer is dutifully printing a document. In such cases, the spool has probably been emptied into the printer's buffer memory, and the result is that the job is no longer under the control of the printing system. To kill such jobs, you need to use the printer's controls (or its power switch!) to stop and delete the job from memory.

16.2.2.1 Managing print queues with lpc

Printer control on Linux includes the oversight of three distinct and independently controlled activities managed by the lpd daemon:

Job queuing

Turn new print jobs on and off.

Printing

Turn on and off the transfer of data to your printer.

lpd child processes

Force the per-queue lpd subprocesses to exit and restart.

lpc can be used in either interactive or command-line form. If lpc is entered without any options, it enters interactive mode and displays its own prompt where lpc commands may then be entered. For example:

# lpc
lpc> help
Commands may be abbreviated.  Commands are:

abort   enable  disable help    restart status  topq    ?
clean   exit    down    quit    start   stop    up
lpc>

If valid commands are included on the command line, lpc responds identically but returns control to the terminal:

# lpc help
Commands may be abbreviated.  Commands are:

abort   enable  disable help    restart status  topq    ?
clean   exit    down    quit    start   stop    up
#

For the discussion that follows, lpc commands are shown as entered on the command line, but results in interactive mode are identical.

lpc

Syntax

lpc
lpc [command]

Description

In the first form, enter interactive mode and accept lpc commands. In the second form, submit command for processing directly from the command line. lpc has no command-line options. Instead, it has commands (see Table 16-3), which are separated here into two groups -- those that affect print queues and those that don't. Most of the commands require a single argument: either the word all (meaning all printers) or a specific printer name.[4]

[4] For this reason, all would be a bad choice for a print queue name!

Table 16-3. Commands for lpc

Command

Description

abort {all|printer}

This command works like stop but terminates printing immediately, even in the middle of a job. The job is retained for reprint when the printer is again started.

disable {all|printer}
enable {all|printer}

These two commands control the queuing of new print jobs. With a queue disabled but printing started, printing continues but new jobs are rejected.

down {all|printer} [message]

disable, stop, and store the free-form message for display by lpr, informing the user why the printer is unavailable.

exit or quit

Terminate lpc's interactive mode.

help

Display help information on commands, as shown earlier.

restart {all|printer}

This command kills and restarts a child lpd, or starts one when none was previously running.

start {all|printer}
stop {all|printer}

These two commands control printing and the child lpd processes. When a stop command is issued, the current print job is allowed to complete. Then the child daemon is stopped and further printing is disabled. start enables sprinting and starts the child lpd if jobs are pending. The print queues remain active.

status [all|printer]

Display queue status. The all|printer argument is optional for this command.

topq name jobs

Place jobs at the top of queue name, behind any active jobs.

up {all|printer}

enable and start.

Example 1

Use the status command to display current printing activity:

# lpc status
lp:
	queuing is enabled
	printing is enabled
	2 entries in spool area
	lp is ready and printing

Example 2

Suppose user jdean has submitted two important print jobs, 206 and 207, and that he needs job 207 to be moved to the top of the queue, followed immediately by 206 (see the emphasized lines in the lpq output). First, examine the existing jobs:

# lpq
Rank   Owner      Job  Files                 Total Size
active root       203  filter                9443 bytes
1st    root       204  status                25 bytes
2nd    root       205  (standard input)      6827 bytes
3rd    jdean      206  (standard input)      403 bytes
4th    jdean      207  cert1.txt             4865 bytes

Now modify the position of print jobs 206 and 207:

# lpc topq lp 207 206
lp:
	moved cfA206AlIwYoh
	moved cfA207Ad6utse

Finally, verify the results:

# lpq
Rank   Owner      Job  Files                 Total Size
1st    jdean      207  cert1.txt             4865 bytes
2nd    jdean      206  (standard input)      403 bytes
3rd    root       203  filter                9443 bytes
4th    root       204  status                25 bytes
5th    root       205  (standard input)      6827 bytes

With this reconfiguration, printing continues with jobs 207 and 206 first, and then reverts to jobs 203 through 205.

Example 3

Disable and enable print queue lp, to allow current printing to complete while rejecting new jobs:

# lpc disable lp
lp:
	queuing disabled
# lpc enable lp
lp:
	queuing enabled

Example 4

Stop and restart printing on the printer attached to queue lp, but allow new jobs to be queued for future printing:

# lpc stop lp
lp:
	printing disabled
# lpc start lp
lp:
	printing enabled
	daemon started  

If no jobs are pending, a child lpd will not start immediately in response to start, though the daemon started message is still displayed. Note also that when a child lpd process is stopped, its subprocesses (such as filters) are also stopped.

Example 5

Abandon all printing activity on the print queue lp. Note that a printer with data in its print buffer may continue to print even after an abort:

# lpc abort lp
lp:
printing disabled
daemon (pid 2012) killed    

On the Exam

You'll need to know the function of each of the lpq, lprm, and lpc commands as well as their options. Note that lpq and lprm (along with lpr, described later) share the -P option to specify a printer. Pay special attention to lpc's syntax, including the all or printer arguments (without one of these, lpc won't do anything). Remember that lpc commands disable/enable handle queues, stop/start handle printing and child lpds, and down/up handle all three. Also remember that lpc commands can be entered on the command line or interactively.

16.2.2.2 Queuing and printing control details

Though it is beyond the scope of the LPIC Level 1 exams, it's interesting to note that lpd uses a crude but tricky way to log the status of queuing and printing. In the spool directory for each print queue, a lock file is written that contains the PID of the child lpd process handling the queue. Normally, this file has permissions 644:

# cd /var/spool/lpd/lp
# ls -l lock
-rw-r--r--   1 root     root            5 Mar 18 19:50 lock

When printing is stopped, the user's execute permission bit is set on this file:

# lpc stop lp
# ls -l lock
-rwxr--r--   1 root     root            5 Mar 18 19:50 lock

When queuing is disabled, the group's execute bit is also set:

# lpc disable lp
lp:
        queuing disabled
# ls -l lock
-rwxr-xr--   1 root     root            5 Mar 18 19:50 lock

These bits act as flags to indicate the status of printing and queuing.