Showing results for 
Search instead for 
Did you mean: 

Script to print to printer + file

Go to solution
Shar Hunter
Frequent Advisor

Script to print to printer + file

Hello All,

We have printers that go down and we then have a hard
time figuring out what printed and what did not. We are running warehousing software, and when an order is printed it changes the order status. But sometimes the printer fails to print, yet the computer thinks it did. To find the lost order I have to run a database query that brings all jobs printed company wide, then take the list and figure out which did not print. There is no way of knowing what printer a job went to.

So I am trying to make a script that will first print to a file, then afterwards print the job to a printer. Then when the printer fails I can look in the file for that printer and know what should have printed and compare it to what actually did print. That way the search is limited to the jobs for that problem printer, instead of every job printed company wide.

Here is the script - with someone else's help.

# /rd/bin/printreports
DA=`date +"%m%d%M%H%S"`
cat $1 > /tmp/printername.$DA
lp -dprintername -s -c /tmp/printername.$DA

Then in the software I change the print comand to run this script instead. It works great! But one problem.

If a job is sent to the same printer as another job within the same second, then the file is overwritten and the other job is lost to the filesystem. It does print out, but no record if it is in the filesystem. I need the record for opening it to read the contents, in the case of a printer failure.

The man pages on 'date' show seconds as the smallest unit. I need a smaller unit.

Or a way to add to the script to have it test to see if a file has already been made that second, if so then to make a file with a different name. This would keep print jobs that happen in the same second to have unique names and not be over written.

I am not very knowledgeable about how to use Unix commands to write scripts. Can anyone suggest a solution this problem? Maybe a way to add to the script so a file is created every time, and not overwriting a previous one sometimes?


I don't think I'm in Kansas anymore.
James R. Ferguson
Acclaimed Contributor

Re: Script to print to printer + file

Hi Troy:

You could use the $RANDOM environmental variable (which returns an integer between 0 and 32767) and append it to your filename, along with the timestamp, if you wish.

You could also use the "$$" variable which returns the PID of the current process. Append this to the filename as you see fit.

A. Clay Stephenson
Acclaimed Contributor

Re: Script to print to printer + file

Hi Troy,

That was a pretty good effort for a novice script writer. I would instead use the process ID of your script.

The PID's do repeat but over very long internals. This is the standard UNIX idiom for creating temp file names.

Regards, Clay
If it ain't broke, I can fix that.
Shar Hunter
Frequent Advisor

Re: Script to print to printer + file

Thanks to both of you!

That is exactly the kind of help I was hoping for. Again, since I am lame at writing scripts - I have one more request.

How do I append the {PID} to the filename plus the time stamp? Just for curiosity.

Unix gives time and date of file creation, so that is good enough without the timestamp - so long as I use the PID trick you guys gave me. This should work well.

Thanks again!
I don't think I'm in Kansas anymore.
Deshpande Prashant
Honored Contributor

Re: Script to print to printer + file

You can use time stamp plus PID
DA=`date +"%m%d%M%H%S"`

The file /var/adm/lp/log also keeps log of all files printed on printers on machine.


Take it as it comes.