Script to identify and clear print queues at OS level

Below script will identify all current print queues and clean up queues older than “1” day(you can change this number as per your retention policy)

[printadm@AM501AMERAP0001 ~]$ cat /tmp/Heema/printclear.sh

!/bin/bash

#

Author : Heema Satapathy

About : Clear Printer Queue

export A=$(date +%j)
mv /tmp/Heema/printqueue.txt /tmp/Heema/printqueue-date +%Y%m%d%H%M%S.txt
mv /tmp/Heema/printqueuename.txt /tmp/Heema/printqueuename-date +%Y%m%d%H%M%S.txt
find /tmp/Heema/ -name “printqueue.” -mtime +5 -exec rm {} \;
rm /tmp/Heema/printclear.out
lpstat -o | awk ‘{print $5 $6 $7}’ > /tmp/Heema/printqueue.txt
lpstat -o | awk ‘{print $1}’ > /tmp/Heema/printqueuename.txt
file1=/tmp/Heema/printqueue.txt
file2=/tmp/Heema/printqueuename.txt
export B=$(date +%Y)
Lines=$(cat $file1)
for Line in $Lines
do
echo “Queue in deletion process was from $Line”
export C=$(date +%j –date “$Line”)
export D=$(date +%Y –date “$Line”)
echo “This queue was on :$C day of $D year”
if [ “$B” = “$D” ]
then
diff=$(($A – $C))
echo “The difference is :$diff days”
echo “The current year is :$D”
if [ $diff -gt 1 ]
then
echo “The queue is older than 1 days during current year, clearing the queue….”
num=1
#E=$(sed -n ‘p’ $file2)
#echo “$E”
F=$(head -n $num $file2)
echo “$F”
cancel -a $F
num=num+1
else
echo “The queue is newer than 1 days during current year, NOT CLEARING THIS QUEUE”
fi
else
echo “There are queues from last year, PLEASE REVIEW AND DELETE THEM MANUALLY”| /usr/sbin/sendmail -t dbasupport@ctrworld.com
fi
echo “The day today is :$A th day out of $B year”
done

[printadm@AM501AMERAP0001 ~]$

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s