CUPS server with rcswitch-pi

My laser printer is placed far away from my desk and automatically connects to my WLAN router when it is switched on. I do not use the printer frequently, so that it is idle most of the time. Recently, I started some experiments controlling the light of my aquarium using a 433 MHz remote switch, which worked like a charm. I thought that it would be nice to save some power by letting the printer start as soon as a new print job arrives to the printer. So here is how it works:

Install CUPS on your Pi

There are several users at home printing from both Win7 or Linux machines. So far, they directly send their print jobs to the printer using the IPP protocol. In order to have a printer queue that can be monitored for new jobs, it is necessary to have a print server in between clients and printer. Therefore, I first installed the cups printer daemon on my RasPi home server. I googled several tutorials how to do that on the net: I followed this guide. To be able to check the printer queue with lpq, it is necessary to install the Debian cups-bsd package with:

$ sudo apt-get install cups-bsd

After configuring the printer, one can query the printer queue with ‘lpq’ and see the following output:

> printer is ready
> no entries

On submission of a new job, it is displayed as the first entry of the queue and marked as “active”. The output of lpq can be parsed with a Python script for switching the printer on demand.

Python lpq watchdog

The Python script below checks if a new job has arrived to the printer queue. It parses the ‘lpq’ output at time intervals of 60 seconds. When a new (active) job is detected, a signal is sent to the rc-switch that connects to the printer. If the queue remains empty for more than 10 minutes, the printer is switched off.

import commands
import time
import os
 
# Time to wait until job is done, 10 minutes 
WAIT = 600 
 
def check(): 
 global QUEUE 
 QUEUE = commands.getoutput("lpq").split("\n")[1].split()[0] 
 return 
 
def active():
 global QUEUE
 QUEUE = commands.getoutput("lpq").split("\n")[2].split()[0]
 return
def wait():#
 global QUEUE
 while (QUEUE != 'no'):
 check()
 time.sleep(WAIT) # wait for the given time in seconds
 return
# Turn socket D on
def start():
 os.system("sudo /usr/local/bin/send 00110 00010 1")
 return
# Turn socket D off
def stop():
 os.system("sudo /usr/local/bin/send 00110 00010 0")
 return
def main():
 global QUEUE
 try:
 while 1:
 time.sleep(60)
 # check the queue for active jobs every minute
 check()
 # if the queue is active, turn socket D on
 if (QUEUE != 'no'):
 active()
 if (QUEUE == 'active'):
 start()
 wait()
 check()
 if (QUEUE == 'no'): # when the queue is empty, turn socket D off
 stop()
except KeyboardInterrupt:
 stop()
if __name__ == '__main__':
 main()

You can download the script over here:

check_lpq.py

Subscribe
Notify of
4 Comments
Inline Feedbacks
View all comments
Rasmus Eriksen
9 years ago

Great script. But something is wrong with the indentions. Could I ask you to mail me the original script?

Thank you
/rasmus

Bart Stegeman
9 years ago
Reply to  luetzel

Hello Martin,

I will try your RC-Switch script on OMV, and I will try to switch 2 printers on with a solid state relay which i drive with a serial port (COM2 DTR-pin).

Is it possible for you to send me the original file also (as you did to Rasmus)

Thank you for your nice blog!!

Kind regards,

Bart Stegeman