LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Write a file, then wait for someone to print it (https://www.linuxquestions.org/questions/linux-software-2/write-a-file-then-wait-for-someone-to-print-it-768440/)

friendlynotnice 11-11-2009 10:49 AM

Write a file, then wait for someone to print it
 
Hi,

the application I am using writes a report file (ascii), but I am not able to print it, because from inside the application I have no access to the shell and so cannot invoke any printing command (lpr, lp, ...). Is there a tool/daemon which waits for a file in a given directory, takes it, and sends it to a printer?

Thanks,
Patrick

pixellany 11-11-2009 11:31 AM

Welcome to LQ!!

I was under the impression that most languages had some kind of "system call" for such things. What language are you using?

regardless, it should be fairly simple to write a small script that continuously scans a folder for a particular filename, and then prints it. e.g., put something like this into an endless loop (use a loop delay of at least 30 seconds to keep your machine from "churning"):
Code:

find . -name words|xargs lp
Rigorously, I imagine that the script would be run in the background.

Disillusionist 11-11-2009 11:44 AM

A couple of things.

First, I would check that the file has finished writing, generally by checking if the file is more than 2 minutes old:
Code:

find . -mmin +2 -name words|xargs lp
Second, I would suggest that after it has printed the file, you may want to move/remove it.

pcunix 11-11-2009 12:34 PM

To check that it is finished writing, I'd use lsof or fuser.

But:

If your app can write to a named pipe or a network socket, it's easy to setup a listener for the socket or a daemon reader for the named pipe - either one will allow instant response rather than watching for the file.

If you need script examples, I can provide for either or both.

Disillusionist 11-11-2009 01:19 PM

I've had inconsistent results from fuser, which is why I tend to check modified time.

Haven't used lsof though it looks interesting.

friendlynotnice 11-12-2009 02:54 AM

Regarding the language: I think it is a MVS-application migrated to *nix, the configuration files are called "JCL" :-)

I just retrieved a thought from the long, dark corridors of my minds' long term storage: isn't there a possibility to create an entry in /dev which really is a shell script that calls my printing commands? I have the feeling that I once read about this ...

pcunix 11-12-2009 05:26 AM

Quote:

Originally Posted by friendlynotnice (Post 3753938)
Regarding the language: I think it is a MVS-application migrated to *nix, the configuration files are called "JCL" :-)

I just retrieved a thought from the long, dark corridors of my minds' long term storage: isn't there a possibility to create an entry in /dev which really is a shell script that calls my printing commands? I have the feeling that I once read about this ...

That's what I mentioned above: a named pipe (doesn't have to be in /dev) with a daemon listening to it.

Like this:
Code:

mknod /dev/myfakenetprint p

That creates a "device" that your application can print to. Use "chmod" as necessary to give it whatever permissions you need (666 if everyone needs to use it).

Now you need something that runs all the time in background. It's a shell script and it needs to start automatically whenever the machine is rebooted:

Code:

while :
do
exec </dev/myfakenetprint
lp -dmyrealnetprinter
done


(On a very busy system, it's possible for your app to send new data to the pipe before your reader process has sent it to the print server. You could implement a cooperative locking system to prevent that, but a small sleep at the end of the interface script might also work. Most often this is not an issue.)

More details at http://aplawrence.com/SCOFAQ/FAQ_scotec7netdevice.html


All times are GMT -5. The time now is 07:06 PM.