System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

CUPS print to unique filename

davep76
Occasional Advisor

CUPS print to unique filename

In Suse 10 sp3, I have a CUPS queue configured to print ascii to a file.

Can the generated file have a random number as part of the filename so it doesnt keep overwriting the same file ?

I tried passing the following to the lpadmin command -v option:

file:/var/opt/rpt/fsp.$$

thinking that the $$ would be replaced with the process ID every time a new output file was generated. Unfortunately this is interpreted once when the lpadmin command is executed. I also tried backslashes and quoting.

Is what I am asking for even possible ?

Thanks
4 REPLIES
Viktor Balogh
Honored Contributor

Re: CUPS print to unique filename

Hi,

I'm not sure if it works, but try it quoting with '

file:'/var/opt/rpt/fsp.$$'

****
Unix operates with beer.
davep76
Occasional Advisor

Re: CUPS print to unique filename

Yep, tried that. The $$ is interpreted once when the lpadmin command is executed. The result is a file with the same name every single time.

My users need a unique filename every time a request is queued.
Viktor Balogh
Honored Contributor

Re: CUPS print to unique filename

That's because the $$ is the PID of the running shell. What if you would use mktemp for generating temporary filenames?

try to feed lpadmin -v with this:

"file:$(mktemp --tmpdir=/var/opt/rpt/ -t fsp.XXX)"

this should do the job:

# echo "file:$(mktemp --tmpdir=/var/opt/rpt/ -t fsp.XXX)"
file:/var/opt/rpt/fsp.MAB



****
Unix operates with beer.
Matti_Kurkela
Honored Contributor

Re: CUPS print to unique filename

The "file:" printer URLs are interpreted internally by cupsd, so a shell is not involved in the printing process.

To get what you want, you may have to write your own CUPS backend. Essentially, you name your own printer URL scheme and create a script (or any executable) in the CUPS backend directory /usr/lib/cups/backend. The backend script name should match the URL scheme name, so if your backend is /usr/lib/cups/backend/davep76, then your printer URL should begin with "davep76:".

The backend executable must fulfill certain requirements: see "man backend" for more information.

Attached is a minimal test backend script. When run without any arguments (like CUPS does when discovering backends and devices) it reports discovery information appropriate for print-to-file style functionality.

If run with arguments, it captures all the arguments to environment variables, and dumps the current environment to /tmp/testbackend.env, so that you can examine both the command line arguments and the environment variables already defined for you by CUPS. (You can remove the command that writes the /tmp/testbackend.env file once you don't need it.)

Normally a CUPS backend must be ready to receive the print job by either reading a named file, or by reading standard input if no filename is given. This test backend arranges for the data to be always available through standard input, to make it simpler to implement the actual job processing.

This test backend simply sends the print job contents to /dev/null. Replace the command "cat > /dev/null" with "cat > /var/opt/rpt/fsp.$$" and you'll have a working backend that should do what you want. CUPS and the backend script both will give you a lot of environment variables which may be useful in choosing the name of the destination file.

The backend receives the full printer URL in an environment variable. Your backend can choose to use it for something, or to ignore it completely. If you choose to ignore the device URL, only the scheme name will be important in defining the printer for CUPS: you can configure a printer with a device URL like "davep76:/meaninglessjunk" and the job will still be passed to the davep76 backend.

MK
MK