Printers and Interfaces

Printers and Interfaces

Under Linux, printing is managed and administered by several commands and files located in various parts of the system. The primary administrative directory is /usr/spool. Each printer that you have configured has its own subdirectory, /usr/spool/lpd/<name>, where <name> is the name of the printer. In this subdirectory, you will find status information about the printer, as well as information about the jobs currently being printed.

The actual printing is done by the lpd daemon. On system start-up, lpd is started through one of the rc scripts (normally somewhere under /etc/rc.d). As it starts, lpd looks through the printer configuration file, /etc/printcap, and prints any files still queued (normally after a system crash).

In each spool directory is a lock file that contains the process id (PID) of the lpd process. The PID helps keep multiple printer daemons from running and potentially sending multiple jobs to the same printer at the same time. The second line in the lock file contains the control file for the current print job.

Management of the print system, or print spool, is accomplished through the lpc utility. This is much more than a “command” because it performs a wide range of functions. One function is enabling printing on a printer. By default, there is probably one printer defined on your system (often lp). The entry is a very simple print definition that basically sends all the characters in the file to the predefined port. (For the default printer on a parallel port, this is probably /dev/lp1.)

When a job is submitted to a local printer, two files are created in the appropriate directory in /usr/spool. (For the default printer, this would be /usr/spool/lp1). The first file, starting with cf, is the control file for this print job. Paired with the cf file is the data file, which starts with df and is the data to be printed. If you are printing a pre-existing file, the df file will be a copy of that file. If you pipe a command to the lpr command, the df file will contain the output of the command. Using the -s option, you can force the system to create a symbolic link to the file to be printed.

The cf file contains one piece of information on each of several lines. The first character on each line is an abbreviation that indicates the information contained. The information contained within the cf file includes the name of the host from which the print job was submitted (H), the user/person who submitted the job (P), the job name (J), the classification of the print job (C), the literal string used on the banner page to identify the user (L), the file containing the data (this is the df file) (f), which file to remove or “unlink” when the job is completed (U), and the name of the file to include on the banner page (N). If you check the lpd man-page, you will find about a dozen more pieces of information that you could include in the cf file. However, this list represents the most common ones.

In the same directory, you will find a status file for that printer. This file is called simply “status” and normally contains a single line such as

printing disabled

If you were to re-enable the printer, the line would then change to

lp is ready and printing

Looking at this line, you might have noticed something that might seem a little confusing. (Well, at least it confused me the first time). That is, we’ve been talking about the directory lp1 all along, but this says the printer is lp. Does this mean that we are talking about two separate printers? No, it doesn’t. The convention is to give the directory the same name as the printer, but there is no rule that says you have to. You can define both the printer name and the directory any way you want.

This is probably a good time to talk about the printer configuration file, /etc/printcap. This file contains not only the printer definitions but the printer “capabilities” as well. In general, you can say the printcap file is a shortened version of the termcap file (/etc/termcap), which defines the capabilities of terminals.

In the printcap file, you can define a wide range of capabilities or characteristics, such as the length and width of each line, the remote machine name (if you are remote printing), and, as we discussed, the name of the spool directory. I will get into shortly what each of the entries means.

As we talked previously, the lpc command is used to manage the print spooler. Not only can you use it to start and stop printing, but you can use it to check the status of all the printer queues and even change the order in which jobs are printed.

There are two ways of getting this information and to manage printer queues. The first is to call lpc by itself. You are then given the lpc> prompt, where you can type in the command you want, such as start, disable, or any other administrative command. Following the command name, you must either enter “all,” so the command will be for all printers, or the name of the printer.

The lpc program will also accept these same commands as arguments. For example, to disable our printer, the command would be

lpc disable lp1

For a list of options, see the lpc man-page. A list of printer queue commands can be found in Table Print Queue Commands.

One aspect of the Linux print system that might be new to you is that you enable or disable the printing functionality within the kernel. Even though printer functionality is configured, you may not be able to print if you have hardware conflicts. When your run ‘make configure’ one of the options is to enable printing.

Once you have added the printer support to the kernel, the first thing you should do is test the connectivity by using the ls command and sending the output to the printer device. This will probably be /dev/lp0, /dev/lp1, or /dev/lp2, which corresponds to the DOS device LPT1, LPT2, and LPT3, respectively. For example, to test the first parallel port you could use

ls > /dev/lp0

What results is:

INSTALL@ dead.letter linux@ lodlin15.txt lodlin15.zip mbox sendmail.cf tests/

However, if you were to issue the command without the redirection, it would probably look like this:

INSTALL@ dead.letter linux@ lodlin15.txt lodlin15.zip mbox sendmail.cf tests/

The reason for this is that the ls command puts a single new-line character at the end of the line. Normally, the shell sees that new-line character and is told to add a carriage return onto the line. However, the printer has been told. Therefore, when it reaches the end of the line with the sendmail.cf, just a new line is sent. Therefore, the printer drops down to the next (new) line and starts printing again. This behavior is called “stair-stepping” because the output looks like stair steps. When a carriage return is added, the shell returns back to the left of the screen as it adds the new line.

Table – Print Queue Commands

Command

Function

lpc

Printer control program

lpd

Print spooler daemon

lpr

Print program

lpq

Print queue administration program

lprm

Remove jobs from print queue

pr

Convert text files for printing