mobile logo

CUPS Purging not-completed print jobs older than..

posted on January 23, 2009 / IN 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.

# Purge print jobs that are not-complete and older than 10 days.

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


for line in os.popen('/usr/bin/lpstat -o').readlines():
    data = line.strip().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]))
By admin

Tagged With


Please use the form to leave a comment

    • Mike
      Posted on June 4, 20105:27 pm Reply

      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.

    • RuBiCK
      Posted on August 27, 20103:02 am Reply

      This script was usefull for me.

      thanks for share it! 😉

    • RuBiCK
      Posted on September 10, 20105:11 am Reply

      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/”, line 25, in ?
      epoch_job = int(time.mktime(time.strptime(date_time, pattern)))
      File “/usr/lib64/python2.3/”, 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

    • Morgan
      Posted on August 16, 201311:03 am Reply

      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 ?

Page 1 of 1

Leave a comment.