1836579 Members
1952 Online
110102 Solutions
New Discussion

A new script is required

 
SOLVED
Go to solution
Nick D'Angelo
Super Advisor

A new script is required

Hello,

I am being asked if it is possible to script a cron job so that it will check our print queues for jobs that have been sitting idle for 15 minutes.

The script is also required to email the person who submitted the print job. I am currently using /etc/mail/aliases on the local machine.

Any suggestions?

Thanks
Always learning
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: A new script is required

First you need to define "idle". Unless that term is precisely defined then you can't begin to write a script.

What you could try is the output of lpstat -o and compare it to the output of 15 minutes ago. This isn't foolproof because some printjobs take much longer than 15 minutes to complete. It also makes a difference if these are Network printers, Remote printers, or directly-attached printers. In the case of Network printers, enabling true end of job might appear to be an answer but that will usually create far more problems than it solves. The mailing part is trivially easy as the lpstat -o output has everything you need (except perhaps a UNIX user to mail address lookup but that can be a simple file).
If it ain't broke, I can fix that.
Nick D'Angelo
Super Advisor

Re: A new script is required

Thanks Clay.

Good point on the idle question.

I would like to do an lpstat -0 every 15 minutes and if the print job was submitted more than 15 minutes ago, to send an email to the submitter: here is an example

istatux /home1/nickd root lpstat -o
kit1-494 root priority 0 Sep 5 11:33
(standard input) 5092 bytes

I sent this job at 11:33 and if I was to have a script that ran now, I would like an email sent to root in this case as root submitted the job.
Always learning
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: A new script is required

You should really do this on your own but this should get you started:

------------------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/bin

typeset PROG=${0##*/}
typeset TDIR=${TMPDIR:-/var/tmp}
typeset TF1=${TDIR}/X${$}_1.lp
typeset TF2=${TDIR}/X${$}_2.lp
typeset -i DELAY=900 # seconds
typeset -i STAT=0
typeset JOB=""
typeset USER=""


trap 'eval rm -f ${TF1} ${TF2}; exit' 0 1 2 3 15

lpstat -o | awk '{if ($1 ~ "^[A-Za-z0-9].+-[0-9]+$") {print $0}}' > ${TF1}
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "${PROG}: lpstat failed; status ${STAT}." >&2
fi
while [[ 1 -eq 1 && ${STAT} -eq 0 ]]
do
sleep ${DELAY}
lpstat -o | awk '{if ($1 ~ "^[A-Za-z0-9].+-[0-9]+$") {print $0}}' > ${TF2}
awk '{print $1}' ${TF1} | while read JOB
do
awk -v job="${JOB}" \
'{if ($1 == job) {print $2; exit(0)}}' ${TF2} | while read USER
do
echo "Send mail to ${USER} about printjob ${JOB}"
done
done
mv ${TF2} ${TF1}
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "${PROG}: mv failed; status ${STAT}." >&2
fi
done
exit ${STAT}

-------------------------------------------
You don't really have to keep up with the times. You simply do an lpstat -o and save the important data to a file, TF1. Wait 15 minutes and do the same operation and capture the lpstat output in TF2. Now read TF1 and see if printername-jobno is still in both files and if so send mail to the user.
Finally mv TF2 to TF1 and repeat. I'll leave the mailing up to you as that part is trivially easy.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: A new script is required

It might make it a bit easier if I attach it so that you don't lose spacing and linefeeds.
If it ain't broke, I can fix that.
Nick D'Angelo
Super Advisor

Re: A new script is required

Clay,

The script seems to hang for some reason, not sure why.

Sorry, I am not much of a scripter so I don't even know where to begin to look.

Always learning
A. Clay Stephenson
Acclaimed Contributor

Re: A new script is required

Of course it seems to hang. It does a sleep 900 (15 minutes) and then looks to see if any of the same jobs are still in the queue and then repeats. It never exits unless an error is encountered or a signal is received.
I suspect your production version will be started/stop by an rc script. For testing purposes reduce the DELAY to something like 15 seconds and intentionally disable one of your print queues.
If it ain't broke, I can fix that.
Nick D'Angelo
Super Advisor

Re: A new script is required

What a dolt I am.

Sorry about that.

Thanks, it works fine.

Thanks Clay.
Always learning