📄 ch20.htm
字号:
deselected or disconnected, this could cause your shell to hang.</P>
<P>What Linux does is spool the data. Spooling means collecting data into a file,
and then starting up a background process to send the data to the printer. There
is a spool area for each printer connected to your machine. Data designated for the
printer is collected in the spool area at the rate of one file per print job. A background
process--called the printer daemon--constantly scans the spool areas for new files
to print. When one appears, the data is removed from the spool area and sent to the
appropriate printer. When more than one file is waiting to be printed, they will
be printed in the order they were completed. The spool area is really a queue.</P>
<P>The printer daemon needs the following information to do its job: the physical
device to use, the spool area to look in, and if printing on a remote machine, the
name of the remote machine and printer for remote printing. All this information
is stored in a file called <TT>/etc/printcap</TT>.</P>
<P>There are five programs that use this information. These programs are in the <TT>/usr/bin</TT>
and <TT>/usr/sbin </TT>directories: <TT>lpr</TT>, <TT>lpq</TT>, <TT>lpc</TT>, <TT>lprm</TT>,
and <TT>lpd</TT>. The first four are used to submit, cancel, and inspect print jobs.
The <TT>/etc/lpd</TT> program is the printer daemon. There are man pages for all
these commands, which you should consult for more information.</P>
<P>The thing to remember is that by default <TT>lpr</TT>, <TT>lprm</TT>, <TT>lpc</TT>,
and <TT>lpq</TT> operate on a printer called <TT>lp</TT>. You can define an environment
variable called <TT>PRINTER</TT> to specify the printer name. For example, to print
to the <TT>hplj</TT> printer, use <TT>lp -Phplj</TT> to override any setting of the
<TT>PRINTER</TT> variable. Here is a quick introduction to some of the key commands
related to printing under Linux:
<UL>
<LI>The <TT>lpr</TT> command submits a job to the printer.
<P>
<LI>The <TT>lpq</TT> command shows you the contents of the spool directory for a
given printer. Each file is specified by an ID and its location in the queue.
<P>
<LI>The <TT>lprm</TT> command removes a job from the printer queue. You have to specify
the ID returned for that job specified by <TT>lpq</TT>.
<P>
<LI>The <TT>lpc</TT> command is the printer control command. This command lets you
check the status of each printer and set its state. You can enable or disable printers
and their queues. If no parameters are specified, <TT>lpc</TT> is in interactive
mode, and you can type in your commands at a prompt. Replies to your commands are
printed immediately. Type a <TT>?</TT> for a list of commands you can use.
</UL>
<P>All these programs work off one directory, usually the <TT>/usr/spool/lpd</TT>.
Each printer has its own area under this directory to spool data in. For my <TT>ps</TT>
printer, I have a <TT>/usr/lpd/spool/lpd/ps</TT> directory.</P>
<P>The printer spool directories should belong to the daemon group and are both user
and group read/writable, and world -readable. That is, the directory has to have
permissions of <TT>-rwxrwxr-x (0775)</TT>.</P>
<P>Each spool directory should contain four files: <TT>.seq</TT>, <TT>errs</TT>,<TT>
lock</TT>, and <TT>status</TT>. These files should have the permissions <TT>-rw-rw-r-</TT>.
The <TT>.seq</TT> file contains the job number counter for <TT>lpr</TT> to assign
a job. The status file contains the message to be reported by <TT>lpc stat</TT>.
The lock file is used by <TT>lpd</TT> to prevent itself from trying to send two jobs
to the same printer at once. The <TT>errs</TT> file is a log of printer failures
and is not required. The status file has a text description of what the <TT>lpd </TT>program
is doing with that file, for example printing, waiting, and so on.
<CENTER>
<H3><A NAME="Heading8<FONT COLOR="#000077">The /etc/printcap File</FONT></H3>
</CENTER>
<P>The file <TT>/etc/printcap</TT> is a text file and is owned by <TT>root</TT>.
The contents of <TT>/etc/printcap</TT> are not the easiest things to read. Each entry
in this file contains a description for a printer and how data is to be handled for
that printer. For example, a printcap entry will define what physical device is to
be used, what spool directory data for that device should be stored in, what preprocessing
should be performed on the data, where errors on the physical device should be logged,
and so forth. You can limit the amount of data that may be sent in a single job,
or limit access to a printer to certain types of users.</P>
<P>You can have multiple printcap entries defining several different ways to handle
data destined for the same physical printer. For example, a physical printer may
support both PostScript and HP LaserJet data formats, depending on some setup sequence
being sent to the physical printer before each job. It would make sense to define
two printers, one of which preprocesses the data by prepending the HP LaserJet sequence,
while the other prepends the PostScript sequence. Programs that generate HP data
would send it to the HP printer, while programs generating PostScript would print
to the PostScript printer.</P>
<P>Programs that change the data before it is sent to the physical printer are called
filters. It is possible for a filter to send no data at all to a physical printer.
An example of such a filter entry in a printcap file is shown in the following lines:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF"># Sample printcap entry with two aliases
myprinter|laserwriter:\
# lp is the device to print to - here the first parallel printer.
:lp=/dev/lp0: \
# sd means `spool directory' - where print data is collected
:sd=/usr/spool/lpd/myprinter:
</FONT></PRE>
<P>Here's a brief summary of some of the entries in <TT>/etc/printcap</TT>. All fields
in each entry are enclosed between a pair of colons and are denoted by a two-letter
code. The two-letter code is followed by a value that depends on the type of field.
There are three types of fields--<TT>string</TT>, <TT>boolean</TT>, and <TT>numeric</TT>.
See Table 20.2 for a listing of some <TT>/etc/printcap</TT> fields. <BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 20.2. Some fields for /etc/printcap. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Code </I></TD>
<TD ALIGN="LEFT"><I>Type </I></TD>
<TD ALIGN="LEFT"><I>Description </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>lp</TT> </TD>
<TD ALIGN="LEFT"><TT>string</TT> </TD>
<TD ALIGN="LEFT">Specifies the device to print to, such as <TT>/dev/lp0</TT> </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>sd</TT> </TD>
<TD ALIGN="LEFT"><TT>string</TT> </TD>
<TD ALIGN="LEFT">Specifies the name of the spool directory for this printer </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>lf</TT> </TD>
<TD ALIGN="LEFT"><TT>string</TT> </TD>
<TD ALIGN="LEFT">Specifies the file to which errors on this printer are to be </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">logged </TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>if</TT> </TD>
<TD ALIGN="LEFT"><TT>string</TT> </TD>
<TD ALIGN="LEFT">Specifies the input filter name </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>rm</TT> </TD>
<TD ALIGN="LEFT"><TT>string</TT> </TD>
<TD ALIGN="LEFT">Specifies the name of a remote printing host </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>rp</TT> </TD>
<TD ALIGN="LEFT"><TT>string</TT> </TD>
<TD ALIGN="LEFT">Specifies the name of a remote printer </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>sh</TT> </TD>
<TD ALIGN="LEFT"><TT>boolean</TT> </TD>
<TD ALIGN="LEFT">Specifies this to suppress headers (banner pages) </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>sf</TT> </TD>
<TD ALIGN="LEFT"><TT>boolean</TT> </TD>
<TD ALIGN="LEFT">Specifies this to suppress end-of-job form feeds </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>mx</TT> </TD>
<TD ALIGN="LEFT"><TT>numeric</TT> </TD>
<TD ALIGN="LEFT">Specifies the maximum allowable print job size (in blocks) </TD>
</TR>
</TABLE>
</CENTER>
<P><BR>
Input filters are programs that take print data on their standard input and generate
output on their standard output. A typical use of an input filter is to detect plain
text and convert it into PostScript. That is, raw text is its input, and PostScript
is its output.</P>
<P>When you specify an input filter, the printer daemon does not send the spooled
print data to the specified device. Instead, it runs the input filter with the spooled
data as standard input and the print device as standard output.</P>
<P>Sending your print data to a printer attached to another machine is done via the
remote machine <TT>rm</TT> field and the remote printer <TT>rp</TT> field. Make sure
that the print device field <TT>lp</TT> is empty. Note that data will still be spooled
locally before being transferred to the remote machine, and any input filters you
specify will also be run.</P>
<P>Suppressing form feeds (<TT>sf</TT>) is most useful if your printer is typically
used for output from word- processing packages. Most WP packages create complete
pages of data, so if the printer daemon is adding a form feed to the end of each
job, you get a blank page after each job. If the printer is usually used for program
or directory listings, however, having that form feed ensures that the final page
is completely ejected, so each listing starts at the top of a new page.</P>
<P>The <TT>mx</TT> field enables you to limit the size of the print data to be spooled.
The number you specify is in BUFSIZE blocks (1KB under Linux). If you specify zero,
the limit is removed, enabling print jobs to be limited only by available disk space.
Note that the limit is on the size of the spooled data, not the amount of data sent
to the physical printer. If a user tries to exceed this limit, the file is truncated.
The user will see a message saying <TT>lpr: <</TT>filename<TT>>: copy file
is too large</TT>. This is useful if you have users or programs that may deliberately
or accidentally create excessively large output. For PostScript physical printers,
the limit is not useful at all because a very small amount of spooled PostScript
data can generate a large number of output pages.</P>
<P>In order for any other machines to print using your printers, their names have
to be registered in either the file <TT>/etc/hosts.equiv</TT> or <TT>/etc/hosts.lpd</TT>.
Both files are simple text files with one host name per line. For security, add hosts
to <TT>/etc/hosts.lpd</TT> only. Do not use <TT>/etc/hosts.equiv</TT> because that
gives more access rights to tasks other than simply sending print jobs out to the
printer.
<CENTER>
<H3><A NAME="Heading9<FONT COLOR="#000077">How It All Fits</FONT></H3>
</CENTER>
<P>Let's go through the steps of setting up printer support on <TT>/dev/lp1</TT>.
Make sure you do this as <TT>root</TT>.
<DL>
<DD><B>1. </B>Create the spool directory for your printer, which we will call <TT>foobar</TT>
for now. Make the permission <TT>-rwxrwxr-x</TT>.<FONT COLOR="#0066FF"></FONT>
</DL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -