1847362 Members
5558 Online
110264 Solutions
New Discussion

old print job removal

 
Craig A. Sharp
Super Advisor

old print job removal

I need to remove print jobs older than 5 days. I would like to do this in perl if possible. Does anyone have any examples of how to do this. We are constantly having to remove jobs that are stale off our print queues manually. With ~1000 printers, this is quite a task and needs to be automated.
9 REPLIES 9
Mel Burslan
Honored Contributor

Re: old print job removal

I am not proficient in perl, hence I will not even attempt to solve your problem, but as a hint,

jobline=`lpstat -o | grep priority`
jobid=`echo $jobline|awk {'print $1'}
day=`echo $jobline|awk {'print $6'}
month=`echo $jobline|awk {'print $5'}
jobtime=`echo $jobline|awk {'print $7'}

usning these variables and using the caljd.sh script, provided by Clay Stephenson (go to http://mirrors.develooper.com/hpux/ and look for words "Date Hammer" to get either shell or perl versions), you can calculate the time past since the request has been submitted and make a decision to delete it or not.

Hope this helps.
________________________________
UNIX because I majored in cryptology...
Geoff Wild
Honored Contributor

Re: old print job removal

Not Perl - but sh:

# cat /usr/local/bin/lpqpurge
#!/bin/sh
#
# script to cancel jobs older then the current month
# run say on the 21st of each month
# gwild

# get the current month

M=`date +%b`

# cancel any jobs older then current month
# lpstat -o returns the following fields
# GB01-7606 ipradm priority 3 Apr 22 04:05 on GB01
# so, compare field 5 to the current month

for JOB in `lpstat -o |grep -v bytes|awk '$5ne"$M"{print $1}'`
do
cancel $JOB
done

# could have been done on 1 line like so:
# lpstat -o |grep -v bytes|awk '$5ne"`date +%b`"{print $1}'|xargs cancel
# but if there are no jobs older then current month - then cancel outputs error



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: old print job removal

Not Perl - but sh:

# cat /usr/local/bin/lpqpurge
#!/bin/sh
#
# script to cancel jobs older then the current month
# run say on the 21st of each month
# gwild

# get the current month

M=`date +%b`

# cancel any jobs older then current month
# lpstat -o returns the following fields
# GB01-7606 ipradm priority 3 Apr 22 04:05 on GB01
# so, compare field 5 to the current month

for JOB in `lpstat -o |grep -v bytes|awk '$5ne"$M"{print $1}'`
do
cancel $JOB
done

# could have been done on 1 line like so:
# lpstat -o |grep -v bytes|awk '$5ne"`date +%b`"{print $1}'|xargs cancel
# but if there are no jobs older then current month - then cancel outputs error



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: old print job removal

Hi Craig:

Firstly, you need to 'cancel' those jobs that you find are candidates. In this way, you keep the lp subsystem "happy".

This Perl script lists the jobs that exist:

#/usr/bin/perl
use strict;
use warnings;
my @requests;
my $request;
@requests=grep{/priority/} `lpstat`;
for $request (@requests) {
print $request;
}

From here, you can enhance the script to snip the request-ID and, using the Perl 'Date::Calc' module's 'Delta_Days' function, calculate the age in days of the request. If it meets your criteria, make a 'cancel' request by calling 'system()'.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: old print job removal

Hi (again) Craig:

Here's a complete working Perl script.

# cat .purgelp
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw( Decode_Month Delta_Days );

my $days_to_remove = @ARGV ? $ARGV[0] : 5;
my @requests;
my @fields;
my ( $line, $month, $age );

my ( $currday, $currmon, $curryr ) = (localtime)[ 3, 4, 5 ];
$currmon++;
$curryr += 1900;

@requests = grep {/priority/} `/usr/bin/lpstat`;
for $line (@requests) {

print $line;

@fields = split /\s+/, $line;
$month = Decode_Month( $fields[4] );
$age = Delta_Days( $curryr, $currmon, $currday, $curryr, $month,
$fields[5] );
if ( $age >= $days_to_remove ) {
system("/usr/bin/cancel $fields[0]");
}
}
1;

...run it passing the number of days at which you want to cancel the print job. By default, without specification, the script assumes 5-days. For example:

# ./purgelp 0
remotep-558 root priority 0 Dec 4 14:30
request "remotep-558" cancelled
remotep-559 root priority 0 Dec 4 14:30
request "remotep-559" cancelled
remotep-560 root priority 0 Dec 4 14:30
request "remotep-560" cancelled

Regards!

...JRF...
Craig A. Sharp
Super Advisor

Re: old print job removal

Great! thanks for the replies...I am not able to install Date::Calc..I am getting the following errors. I do have gcc installed and in the path first.

CPAN.pm: Going to build S/ST/STBEY/Bit-Vector-6.4.tar.gz

Checking if your kit is complete...
Looks good
Writing Makefile for Bit::Vector
Writing patchlevel.h for /opt/perl_32/bin/perl (5.008007)
cp lib/Bit/Vector/Overload.pm blib/lib/Bit/Vector/Overload.pm
cp Vector.pm blib/lib/Bit/Vector.pm
cp Vector.pod blib/lib/Bit/Vector.pod
cp lib/Bit/Vector/String.pod blib/lib/Bit/Vector/String.pod
cp lib/Bit/Vector/Overload.pod blib/lib/Bit/Vector/Overload.pod
cp lib/Bit/Vector/String.pm blib/lib/Bit/Vector/String.pm
cc -c -D_POSIX_C_SOURCE=199506L -D_REENTRANT -Ae -D_HPUX_SOURCE -Wl,+vnocompatwarnings +Z -DUSE_SITECUSTOMIZE -DNO_HASH_SEED -D_LARGEFILE_SOURCE -D_FI
LE_OFFSET_BITS=64 -fast +Onolimit +Opromote_indirect_calls +DAportable +DS2.0 -DVERSION=\"6.4\" -DXS_VERSION=\"6.4\" +Z "-I/opt/perl_32/lib/5.8.7/PA-RISC1.1-
thread-multi/CORE" BitVector.c
(Bundled) cc: warning 480: The -A option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 480: The +Z option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 422: Unknown option "f" ignored.
(Bundled) cc: warning 480: The +Onolimit option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 480: The +Opromote_indirect_calls option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 480: The +Z option is available only with the C/ANSI C product; ignored.
(Bundled) cc: "ToolBox.h", line 40: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 40: error 1000: Unexpected symbol: "char".
(Bundled) cc: "ToolBox.h", line 41: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 41: error 1000: Unexpected symbol: "char".
(Bundled) cc: "ToolBox.h", line 42: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 41: warning 525: Redeclaration of identifier "signed".
(Bundled) cc: "ToolBox.h", line 42: error 1000: Unexpected symbol: "short".
(Bundled) cc: "ToolBox.h", line 43: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 43: error 1000: Unexpected symbol: "short".
(Bundled) cc: "ToolBox.h", line 44: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 44: error 1000: Unexpected symbol: "int".
(Bundled) cc: "ToolBox.h", line 45: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 45: error 1000: Unexpected symbol: "int".
(Bundled) cc: "ToolBox.h", line 46: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 46: error 1000: Unexpected symbol: "long".
(Bundled) cc: "ToolBox.h", line 47: warning 5: "signed" will become a keyword.
(Bundled) cc: "ToolBox.h", line 47: error 1000: Unexpected symbol: "long".
(Bundled) cc: "ToolBox.h", line 71: error 1000: Unexpected symbol: "*".
(Bundled) cc: error 2017: Cannot recover from earlier errors, terminating.
*** Error exit code 1

Stop.
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible
Running make for S/ST/STBEY/Date-Calc-5.4.tar.gz
Is already unwrapped into directory /.cpan/build/Date-Calc-5.4

CPAN.pm: Going to build S/ST/STBEY/Date-Calc-5.4.tar.gz

cp lib/Date/Calc/Object.pod blib/lib/Date/Calc/Object.pod
cp lib/Date/Calendar/Year.pm blib/lib/Date/Calendar/Year.pm
cp Calc.pod blib/lib/Date/Calc.pod
cp lib/Date/Calendar/Profiles.pm blib/lib/Date/Calendar/Profiles.pm
cp lib/Date/Calc/Object.pm blib/lib/Date/Calc/Object.pm
cp Calendar.pod blib/lib/Date/Calendar.pod
cp lib/Date/Calendar/Year.pod blib/lib/Date/Calendar/Year.pod
cp lib/Date/Calendar/Profiles.pod blib/lib/Date/Calendar/Profiles.pod
cp Calendar.pm blib/lib/Date/Calendar.pm
cp Calc.pm blib/lib/Date/Calc.pm
/opt/perl_32/bin/perl /opt/perl_32/lib/5.8.7/ExtUtils/xsubpp -typemap /opt/perl_32/lib/5.8.7/ExtUtils/typemap -typemap typemap Calc.xs > Calc.xsc && mv
Calc.xsc Calc.c
cc -c -D_POSIX_C_SOURCE=199506L -D_REENTRANT -Ae -D_HPUX_SOURCE -Wl,+vnocompatwarnings +Z -DUSE_SITECUSTOMIZE -DNO_HASH_SEED -D_LARGEFILE_SOURCE -D_FI
LE_OFFSET_BITS=64 -fast +Onolimit +Opromote_indirect_calls +DAportable +DS2.0 -DVERSION=\"5.4\" -DXS_VERSION=\"5.4\" +Z "-I/opt/perl_32/lib/5.8.7/PA-RISC1.1-
thread-multi/CORE" Calc.c
(Bundled) cc: warning 480: The -A option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 480: The +Z option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 422: Unknown option "f" ignored.
(Bundled) cc: warning 480: The +Onolimit option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 480: The +Opromote_indirect_calls option is available only with the C/ANSI C product; ignored.
(Bundled) cc: warning 480: The +Z option is available only with the C/ANSI C product; ignored.
cpp: "/opt/perl_32/lib/5.8.7/PA-RISC1.1-thread-multi/CORE/perlio.h", line 108: error 4065: Recursion in macro "PerlIO".
*** Error exit code 1

Stop.
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible
Geoff Wild
Honored Contributor

Re: old print job removal

I know this isn't that relevant, but
Perl is a great tool - but for simple things - why use all that overhead?

My sh script is only 5 lines of code (could be one) and much more efficient (doesn't have to make extra library calss, etc).

Just my 2 cents.

Obivously, no points for this post.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: old print job removal

Hi Craig:

It appears that your version of Perl was built using HP's compiler. If you do:

# perl -V

...and look under the section headed "Compiler" you should see that.

You can fetch and install a 'gcc' compiled Perl in the event that you don't have an HP ANSI C compiler available:

http://mirrors.develooper.com/hpux/

Regards!

...JRF...
Tor-Arne Nostdal
Trusted Contributor

Re: old print job removal

don't know why you insist of doing it in perl... but I have some old shell scripts that will do the work for you.

It was working for me many yrs when we spooled on HP-UX, nowadays we use cheap SuSe lnx with cups as spool servers.
Simplifies also administration for non-unix personell which do this via browser...
It's quite expensive to use HP-UX processing for printout.

I used to run the CancelOldPrint script in cron. It ran from one central server and removed print more than 8 days old.
The monthdays, yeardays and ymd2yd is some old hints I picked up some place... It helps calculating age and might be useful for other purpose as well.

/Tor-Arne
I'm trying to become President of the state I'm in...