23 Jan 2009

CUPS Purging not-completed print jobs older than.. 

By - Linux, Quick Tip 4 Comments

When working with CUPS using remote print queues, you may find that the remote printer is not always available, has timed out, ran out of paper, has a paper jam, etc. Sometimes this causes a job to ‘not-complete’ and when running ‘lpstat -o’, you see old print jobs.

We put together a quick script to auto-purge these jobs.  Hopefully this helps someone in the future.

Please let us know if you found this useful, or if you have any suggested changes.

#!/usr/bin/python
#
# Purge print jobs that are not-complete and older than 10 days.
#

from datetime import date, timedelta, datetime
import time
import os

howmanydaysago=10

today=date.today()
daysago=today-timedelta(days=howmanydaysago)
epoch_today=time.mktime(today.timetuple())
epoch_daysago=time.mktime(daysago.timetuple())
date_difference=epoch_today-epoch_daysago

for line in os.popen('/usr/bin/lpstat -o').readlines():
    data = line.strip().split()
    job=data[0].split('-')

    #15 Apr 2008 12:00:00
    date_time=data[4]+" "+data[5]+" "+data[6]+" 12:00:00"
    pattern = '%d %b %Y %H:%M:%S'
    epoch_job = int(time.mktime(time.strptime(date_time, pattern)))

    difference=epoch_today - epoch_job

    if difference > date_difference:
        print "Canceling pending job: "+str(job[-1])
        os.popen('/usr/bin/cancel '+str(job[-1]))

4 Responses to “CUPS Purging not-completed print jobs older than..”

  1. Mike says:

    I tried this tool to purge old cups print jobs. I made a couple of tweaks, but it appears to work well. Good job, and thanks.

  2. RuBiCK says:

    This script was usefull for me.

    thanks for share it! ;)

  3. RuBiCK says:

    This script works fine but when I try to put into crontab I recieve the next error:

    ——
    Traceback (most recent call last):
    File “/tools/scripts/purga_printers.py”, line 25, in ?
    epoch_job = int(time.mktime(time.strptime(date_time, pattern)))
    File “/usr/lib64/python2.3/_strptime.py”, line 424, in strptime
    raise ValueError(“time data did not match format: data=%s fmt=%s” %
    ValueError: time data did not match format: data=Aug 30 09:07:24 12:00:00 fmt=%d %b %Y %H:%M:%S
    ——

    I don’t know where is the problem. It’s necessary set any enviroment variable or whatever?

    Many thanks

  4. Morgan says:

    I also have the same issue. The script works wonderfully when ran from the command line; however, when run from the crontab it fails with the same errror RuBiCK listed above. Any ideas ?

Leave a Reply