Controlling Linux Printers from the Command Line

In the VDI environment that I administer I have printers attached to discreet Linux-based print servers and VDI clients.

I have previously written about:

In this article I will share with you some of the commands that I use and then share a script framework that I use for error checking.

Most Linux distributions use the Common UNIX Printing System (CUPS) to handle local and remote printing. CUPS uses the Internet Printing Protocol (IPP) as the basis for managing print jobs and queues.

Cups is free software that is licensed under the Apache Licensing. It is very mature as it has been around for over two decades. It supplies a print spooler and scheduler, a filter system. Filter system converts the print data to a format that the printer can print.

Although CUPS has a web-based administration interface I tend to SSH into the systems and manage them from the command line.

Printer Management Commands
Printers can be managed using various command line commands, such as lpstat, cupsdisable, lp and so on. Below are some examples of these commands.

  • To get the status of the printers enter lpstat -r.
  • To list completed print jobs, enter lpstat -W completed.
  • Disabling the default print server requires the name of the printer. This is acquired using the lpstat -d command and then using the cupsdisable command. This the code that I used for this is:
    DP=$(lpstat -d | awk 'BEGIN{FS=":"}{print $2}')
    cupsdisable $DP
    echo "Disabling $DP"
  • Enabling the default print server requires the name of the printer so the above script is used but replaces cupsdisable $DP with cupsenable $DP.
  • To display the status of all printers and all jobs that are submitted by all users enter lpstat -t.
  • To list all the printers on a system, enter lpstat -a.
  • To print a test page on a printer some text must be created and then that text is sent to the printer. The code that I used for this is:
          echo "Printer Test Page" > /tmp/PrintTestPage
          hostname > /tmp/PrintTestPage
          ip address >> /tmp/PrintTestPage
          lpstat -a >> /tmp/PrintTestPage
          lpstat -W completed >> /tmp/PrintTestPage
          lpr /tmp/PrintTestPage
          echo "Check printer for test page"

Sample Script
As I tend to use the commands above quite frequently I created a script frame work to run them. As I don’t know exactly what has been installed on the system the scripts check to make sure that the commands are available on the system and will not run the command if they are not present.

  ## Variables
  DBug=0 ## Set this var to 1 to debug this program.
  AR=0 ## Program will set flag to 1 if command is not to be ran
  if ((DBug == 1)); then echo "Debugging (DBug) is set to $i and is enabled"; fi
  if ((DBug == 1)); then cat /etc/*-release | egrep "PRETTY" ; fi
  ## Start of code
  ## Check if commands are available on the system. If commands are not available, 
        set the flag not to run them.
  for i in lpstat awk cupsdisable lpq
    command -v $i >/dev/null 2>&1 || { MyOut="Script requires $i but it's not installed.
        Aborting."; AR=1;  }
    if ((DBug == 1)); then echo "checking $i and the value of the AR flag which is $AR";
  ## Run logic and output to standard out or print out an error message 
  if ((AR == 0)); then
    DP=$(lpstat -d | awk 'BEGIN{FS=":"}{print $2}')
    cupsdisable $DP
    echo "Disabling $DP"
    echo $(lpq $DP)
    echo "$MyOut"

The CUPS system on Linux systems is very powerful and using the commands above I have been able to efficiently administer it.

About the Author

Tom Fenton has a wealth of hands-on IT experience gained over the past 25 years in a variety of technologies, with the past 15 years focusing on virtualization and storage. He currently works as a Technical Marketing Manager for ControlUp. He previously worked at VMware as a Senior Course Developer, Solutions Engineer, and in the Competitive Marketing group. He has also worked as a Senior Validation Engineer with The Taneja Group, where he headed the Validation Service Lab and was instrumental in starting up its vSphere Virtual Volumes practice. He's on Twitter @vDoppler.