1. Overview

Common Unix Printing System (CUPS) is a useful component of Linux systems. Particularly, it facilitates printing tasks and maintains a comprehensive record of completed print jobs.

In this tutorial, we’ll delve into various methods to access and interpret this job history. In addition, we’ll see various methods to check the job history for many users.

Mainly, we’ll focus on the lpstat command and utilize its numerous options. Last but not least, we’ll look at elaborative code snippets and understand their outputs.

2. Understanding lpstat

lpstat is a versatile command-line tool used in Unix-like systems to provide information about the status of print jobs and printers managed by the Common Unix Printing System (CUPS). Moreover, it serves as a powerful utility for monitoring and managing printing tasks.

In fact, lpstat has various applications:

  • viewing print queue: check the status of print jobs in the queue, including IDs, users, and completion status
  • monitoring printer status: get status of printers, indicating whether they’re enabled, disabled, or accepting jobs
  • canceling print jobs: cancel specific print jobs if needed
  • filtering by user or job ID: filter print jobs based on specific criteria with options like -u and -j

Next, let’s get familiar with some of the most used options with the lpstat command:

  • -t: displays a summary of all printers and their status
  • -o: displays information about all jobs, including their status and owner
  • -u: shows jobs submitted by a specific user
  • -j: provides detailed information about a specific job
  • -W: filters jobs by status

In the following sections, we’ll use some of these commands to meet certain requirements.

3. Displaying Completed Jobs

To begin with, we’ll focus on the way to retrieve information about completed print jobs:

$ lpstat -W completed
Job ID    User    Size (bytes)    Date                  Job Name
---------------------------------------------------------------------- 
123       john    1024            2023-11-01 10:30:00    report.pdf
124       jane    2048            2023-11-02 11:45:00    presentation.pptx
125       jerry   4096            2023-11-03 12:15:00    memo.doc

In the above snippet, we used the -W option followed by the keyword completed. By default, if we didn’t specify any keywords, it would be set to not-completed. Therefore, we should type completed as we’re searching for the completed jobs only.

Next, let’s break down the columns displayed in the output of the lpstat command:

  • Job ID: this is a unique identifier assigned to each print job
  • User: it shows the username associated with the print job
  • Size: this column indicates the size of the print job in bytes
  • Date: it provides the date and time of the completed job
  • Job Name: displays the name assigned to the print job

In detail, if we take a deeper look at the first row, we can figure out that Job ID 123 was initiated by the user john, with a file size of 1024 bytes, and was completed on November 1st at 10:30 AM. report.pdf  is the name of the job.

Alternatively, if we’re looking for a certain job and we cannot find it in the completed jobs, we might want to look for it in the canceled jobs list:

$ lpstat -l -W all -o

This command displays all canceled jobs. We utilize the -l option for long listing the output. Furthermore, we use the -o option to look for aborted jobs by the print system itself, due to driver or other problems such as user or admin cancelation. In our case, there were no canceled jobs.

4. User Filtration

For a more focused view, we can use the -u option to filter the output and view completed jobs for specific users:

$ lpstat -W completed -u user1,user2
Job ID    User    Size (bytes)    Date                  Job Name
---------------------------------------------------------------------- 
126       user1   8192            2023-11-04 13:30:00    report2.pdf
127       user2   16384           2023-11-05 14:45:00    presentation2.pptx

In the above example, we utilized the lpstat command to search for completed jobs. However, this time our objective was to get the completed or successful jobs issued by specific users. In our case, these users were user1 and user2. Hence, we utilized the -u option.

The -u option simply enables us to get a tailored output of the lpstat command by providing one or more users as arguments. In case of listing more than one user, we make sure to provide the correct user names separated by a comma.

Accordingly, the above command displays completed print jobs only for user1 and user2.

On the other hand, for a comprehensive view, we can use the -u all option to display completed jobs for all users:

$ sudo lpstat -W completed -u all
Job ID    User    Size (bytes)    Date                  Job Name
---------------------------------------------------------------------- 
126       user1   8192            2023-11-04 13:30:00    report2.pdf
127       user2   16384           2023-11-05 14:45:00    presentation2.pptx
131       user3   262144          2023-11-09 18:45:00    memo3.doc
132       user4   524288          2023-11-10 20:00:00    report4.pdf
133       user5   1048576         2023-11-11 21:15:00    presentation4.pptx

This command displays completed print jobs for all users on the system.

To focus on a specific subset of completed jobs, we can use commands like head or tail in combination with the lpstat command:

$ sudo lpstat -W completed -u saml,root | head -3
Job ID    User    Size (bytes)    Date                  Job Name
---------------------------------------------------------------------- 
128       saml    32768           2023-11-06 15:00:00    memo2.doc
129       root    65536           2023-11-07 16:15:00    report3.pdf
130       saml    131072          2023-11-08 17:30:00    presentation3.pptx

If a certain user doesn’t have the privilege of executing such commands, we utilize the sudo command to get superuser privileges. Furthermore, we piped the output using the pipe symbol |. This is to provide a temporary connection between the lpstat command and the head command.

Consequently, we used head followed by -3 to indicate that we want to print only the first three lines of the output of both commands combined.

5. Using the Web Interface of CUPS

In this section, we’ll explore alternative methods to access job history, such as using the web interface.

The web interface of CUPS is a user-friendly commonly used tool to access and manage printers and printing through our browser. Simply, it’s a Graphical User Interface, commonly known as GUI, that provides a more streamlined environment when compared to the Command Line Interface (CLI) for performing quick and straightforward tasks.

In our case, to administer the connected printers using the CUPS web interface, we type http://localhost:631/ into our browser from the machine that hosts CUPS. In essence, we use the localhost followed by port number 631, which is the Internet Printing Protocol (IPP) port.

For example, we may need a specific URL to get the completed jobs on our printer from a web browser:

http://localhost:631/jobs?which_jobs=completed

This is in case we only manage a single printer. However, if we have multiple printers that we manage, we need to specify the printer name within this URL. For example, our URL would follow a format indicating the printer followed by the printer’s name:

https://localhost:631/printers/[NameOfPrinter]?which_jobs=completed

Similarly, we can get information on the printer’s status and others.

6. Checking Page Log File

Apart from using the lpstat command and the web interface to manage printed jobs, we can check the log files for a read-only view.

In particular, we find information about completed jobs in log files like /var/log/cups/page_log. This log contains a comprehensive history of print jobs, including timestamps, user details, job names, and more:

$ sudo cat /var/log/cups/page_log
DeskJet root 1 [20/May/1999:19:21:06 +0000] total 2 acme-123
      localhost myjob na_letter_8.5x11in one-sided
....

In the above example, we use the cat command to list the content of the page_log log file. Moreover, the page_log file lists the total number of pages or sheets printed.

7. Handling Special Cases

In some cases, there might be some users listed in the LPD control files that aren’t visible when using the -u all option.

In essence, LPD files are control files used by the LPD (Line Printer Daemon) printing system to manage print jobs on Unix-like systems. Moreover, they contain information about print jobs, such as the user, file to be printed, and printer destination.

Hence, by examining these control files, we can extract usernames associated with print jobs:

$ sudo strings /var/spool/cups/* | grep -A 1 job-originating-user-name | head -5
job-originating-user-name
DanB
--
job-originating-user-name
DanB

Let’s understand the above snippet:

  • strings extracts human-readable text from binary files
  • /var/spool/cups/* is the directory holding control files
  • grep -A 1 job-originating-user-name searches for lines containing job-originating-user-name and includes the line that follows it to help us extract the usernames associated with print jobs
  • DanB is an example username from the control files and represents the user associated with a specific print job

Next, let’s refine the whole list of users:

$ sudo strings /var/spool/cups/* | grep -A 1 job-originating-user-name | \
    grep -oP '.*(?=B)' | sort -u
ethan
Dan
root
sam
saml
slm

This command uses a Perl-compatible regular expression (-P) to extract everything (.*) up to the first occurrence of the letter B. This effectively isolates the usernames. Finally, the code sorts the usernames in alphabetical order by using the sort command and displays unique entries by using the -u option.

This sequence of commands and their respective outputs enable us to effectively extract and refine a list of usernames associated with print jobs in the CUPS system.

Next, let’s combine these users to get a list of the completed jobs:

$ sudo lpstat -W completed -u $(strings /var/spool/cups/* | \
    grep -A 1 job-originating-user-name | \
    grep -oP '.*(?=B)' |sort -u | paste -sd ',')
mfc-8480dn-1525         dan           545792   Thu 28 Nov 2013 01:36:59 PM EST
mfc-8480dn-1526         saml            699392   Sat 30 Nov 2013 10:34:34 AM EST
mfc-8480dn-1652         root              1024   Tue 28 Jan 2014 01:19:34 AM EST

Now, this command provides a detailed list of completed print jobs, including job identifiers, usernames, sizes, and completion timestamps.

8. Conclusion

In this article, we explored methods to access and interpret CUPS printer job history in Linux using the lpstat command.

In addition, we discussed filtering options and alternative techniques. Finally, we examined log files for detailed information and went over specific cases.

At this point, we have a comprehensive understanding of monitoring print activity on our system.