ciphermethod.com

20140218: qpurge.sh

by on Feb.18, 2014, under Linux, Scripts


#!/bin/bash

# Created 20131119 by Jamey Hopkins
# Cancel all jobs older than X days.
#
# Example: qpurge.sh 1 will cancel jobs older than prior day
# qpurge.sh 0 will cancel jobs older than current day
# Note: Use a regular cancel -a to cancel all jobs.
# 20131217 JAH – Display before and after counts.
# – Echo Checking queues and canceling job #.
# – Check for at least 2 files. There should be a control file and at least 1 data.
# 20140114 JAH – Exit if lpstat not found.
# 20140124 JAH – Strip preceding zeroes in job id
# 20140218 JAH – Added section for a plain cancel -a of specific queues
#

if [ “$1” = “” ]; then
   echo Enter days to cancel.
   echo Example: qpurge 1
   exit 1
fi

if [ `id -un` != “root” ]; then
   echo “Need to be root.”
   exit 1
fi

if [ ! `which lpstat` ]; then
   exit 1
fi

X=$1

echo “Checking queues…”

BEFORE=`lpstat -o | wc -l`

# cancel all jobs for specific queues here
#CA=”q1 q2 q3″
if [ -n “$CA” ]; then
   echo “Canceling all jobs on: $CA”
   cancel -a $CA >/dev/null 2>&1
fi

# clear jobs by age
find /var/spool/cups/d* -daystart -mtime +$X >qpurge.tmp 2>/dev/null
for FILE in `cat qpurge.tmp`
do
   JOB=`basename $FILE | sed ‘s/d//g’ | cut -f1 -d-`
   # strip preceding zeroes
   JOB=`echo $JOB | sed ‘s/^0*//’`
   INQ=””;INQ=`lpstat -o | grep — “-$JOB”`
   if [ ! -e c${JOB}* -a “$INQ” != “” ]; then
      echo Canceling $JOB
      cancel $JOB >/dev/null 2>&1
   fi
done

AFTER=`lpstat -o | wc -l`
echo;echo “Jobs Remaining ($AFTER of $BEFORE):”
lpstat -o

rm qpurge.tmp

echo

No comments for this entry yet...

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

But Wait, There's More!

A few highly recommended friends...