How to View All Printer Jobs in CUPS from the Command Line

Managing printer jobs in CUPS (Common Unix Printing System) via the command line offers powerful control and monitoring capabilities. For system administrators and advanced users, accessing a comprehensive list of completed printer jobs, especially across all users, is often necessary. While the lpstat command is a go-to tool for printer status, retrieving a complete history of printer jobs for every user requires a bit more finesse than simply using lpstat -u all.

Understanding lpstat and User-Specific Printer Jobs

The lpstat command is fundamental for querying printer status and job information in CUPS. To view completed jobs for a specific user, you can use the -u option followed by the username. For example, to see completed jobs for the user saml, the command would be:

$ sudo lpstat -W completed -u saml | head -2
mfc-8480dn-1524 saml 23552 Thu 28 Nov 2013 10:45:44 AM EST
mfc-8480dn-1526 saml 699392 Sat 30 Nov 2013 10:34:34 AM EST

This command effectively lists completed printer jobs associated with saml. However, the seemingly straightforward option -u all, intended to display jobs for all users, might not yield the expected results, sometimes returning no output or not capturing all users.

Listing Printer Jobs for All Users: Alternative Approaches

To overcome the limitations of lpstat -u all and ensure a comprehensive view of printer jobs, including those from users not locally registered on the system, we can employ alternative strategies leveraging both user account information and CUPS spool data.

Utilizing getent passwd for Local User Jobs

One approach is to dynamically generate a list of local system users and then query lpstat for each user. This can be achieved by combining getent passwd to retrieve user information and awk and paste to format the usernames for the -u option:

$ sudo lpstat -W completed -u $(getent passwd | awk -F: '{print $1}' | paste -sd ',')

This command effectively constructs a comma-separated list of usernames from /etc/passwd and passes it to lpstat -u. While this method works for users with local accounts, it misses printer jobs initiated by users who don’t have accounts on the system but still print through CUPS.

Leveraging CUPS Spool Directory for a Broader User Scope

CUPS stores job control files in its spool directory, typically /var/spool/cups/. These files contain valuable information, including usernames, even for users without local system accounts. By examining these files, we can extract a more comprehensive list of users who have submitted printer jobs.

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

By parsing these files using commands like strings, grep, awk, sort, and paste, we can create a list of usernames directly from the CUPS spool data:

$ 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 tammy 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
mfc-8480dn-1672 saml 1024 Sun 09 Feb 2014 01:56:26 PM EST

This approach provides a more inclusive list of printer jobs, capturing activity from a wider range of users, regardless of their local system account status.

Conclusion

Viewing all printer jobs in CUPS from the command line requires understanding the nuances of lpstat and exploring alternative data sources. While lpstat -u username is effective for individual users, and combining lpstat with getent passwd covers local users, parsing the CUPS spool directory offers the most comprehensive method for listing printer jobs across all users, providing a valuable tool for system administrators managing printing environments.

References

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *